2

i have a small problem with some code. 2 selects that are populated through the code below

var data_business_type = [
{"category": "Automotive","type": "Please Select Type"}, 
{"category": "Automotive","type": "Auto Accessories "},
{"category": "Real Estate","type": "Please Select"},
{"category": "Real Estate","type": "Apartment & Home Rental"}/*problem here*/
];/*around 150 more 'types' in total*/

$('#r_businessCategory').append('<option selected>Please Select Category</option>');
$('#r_businessType').append('<option selected>Please Select Type</option>');
$('#r_businessOther').hide();

var r_categories = [];
 $.each(data_business_type, function(index, item) {
  if (r_categories.indexOf(item.category) < 0) {
   r_categories.push(item.category);
 };
});

$.each(r_categories, function(index, item) {
 $('#r_businessCategory').append('<option value=' + item + '>' + item + '</option>');
});

$('#r_businessCategory').on('change', function() {
 $('#r_businessType').empty();
  var business_types = [];
$.each(data_business_type, function(index, item) {
 if (business_types.indexOf(item.type) < 0 && $('#r_businessCategory').val() == item.category) {
  business_types.push(item.type);
 };
});

it works at it should except when i want the 'category' to have multiple words. jsfiddle

i'm still trying to learn so i'm not sure how to use all the brackets yet, if that changes anything.I've tried looking for a similar problem but when i try the solutions, it messes with different parts, so I haven't found a working solution yet. And sorry if it's hard to read.

Any help would be appreciated

3
  • Updated your code by adding the quotes at two places: jsfiddle.net/rth1m59j/2 Commented Jan 25, 2017 at 18:56
  • @vyx.ca: Why not add that as an answer rather than a comment? Commented Jan 25, 2017 at 19:00
  • 1
    @Chris Because while I was working on this, Barmar had just posted the correct answer. I felt the corrected code could help the person that asked the question so I posted the link instead of "polluting" the answer board with the exact same thing as Barmar said. Commented Jan 25, 2017 at 19:15

2 Answers 2

5

If an attribute value contains spaces (or some other special characters), you have to put it in quotes. It's generally a good ideal to always put quotes around attribute values.

$('#r_businessCategory').append('<option value="' + item + '">' + item + '</option>');

You can also use a jQuery function to create the element using structured arguments.

$('#r_businessCategory').append($('<option>', { value: item }));
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, that's exactly what i needed
0

The issue is when you call val() it doesn't get anything after the space. A quick fix, is to convert spaces to underscores, and then do it back again. I'm sure someone else will have a better idea, but this works.

var data_business_type = [{
  "category": "Automotive",
  "type": "Please Select Type"
}, {
  "category": "Automotive",
  "type": "Auto Accessories "
}, {
  "category": "Real Estate",
  "type": "Please Select"
}, {
  "category": "Real Estate",
  "type": "Apartment & Home Rental"
}];
$('#r_businessCategory').append('<option selected>Please Select Category</option>');
$('#r_businessType').append('<option selected>Please Select Type</option>');
$('#r_businessOther').hide();

var r_categories = [];
$.each(data_business_type, function(index, item) {
  if (r_categories.indexOf(item.category) < 0) {
    r_categories.push(item.category);
  };
});

$.each(r_categories, function(index, item) {
  $('#r_businessCategory').append('<option value=' + item.replace(" ", "_") + '>' + item + '</option>');
});

$('#r_businessCategory').on('change', function() {
    alert($('#r_businessCategory').val());
  $('#r_businessType').empty();
  var business_types = [];
  $.each(data_business_type, function(index, item) {
    if (business_types.indexOf(item.type) < 0 && $('#r_businessCategory').val().replace("_", " ") === item.category) {
      business_types.push(item.type);
    };
  });
  $.each(business_types, function(index, item) {
    $('#r_businessType').append('<option value=' + item + '>' + item + '</option>');
  });
  $('#r_businessType').append('<option value=Other>Other</option>');
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.