-1

I have a this select dropdown:

<select id="category">
  <option value="cat1">Category 1</option>
  <option value="cat2">Category 2</option>
</select>

and I have this code that everytime I select something form the dropdown it takes the value and find the same class and show it on the last line

$('#category').change(function () {
    var selected_category = $(this).val();

    $('#sub-category').find('.option').hide();
    $('#sub-category').find('.'+selected_category).show();                              
});

I just want to change instead of class so it will find the attribute rel

** edited **

Nelson Denver
  • 115
  • 1
  • 3
  • 12

2 Answers2

2

You definitely don't need jQuery for this. Here's the code to find all option tags with a rel attribute of "sub-cat1":

document.querySelectorAll('option[rel="sub-cat1"]')

The above will work in IE8 and up, which should satisfy all browsers you need to support. The jQuery solution is similar, and uses the same selector string:

$('option[rel="sub-cat1"]')

Furthermore, you don't need to add a class of "option". Simply select using the option tag name.

Ray Nicholus
  • 19,538
  • 14
  • 59
  • 82
1

There can be an alternate way to your answer. Using jquery try ,

$       (".classname").each(function(){  
//iterate over similar *classname* elements one by one
$(this).someFunc(); //it will help you to preform operation at current value while iteration
});

With rel if you want to iterate over rel tag values , try

$("[rel='sub-cat-1']").each(function(){
//Some operation
});
Santosh D
  • 1,172
  • 2
  • 7
  • 5