0

I need a way to loop through all options in a dropdown list via code, and compare each one to a variable. if they match, i want make the matching value be the item that is selected in the dropdown, and then exit the loop. So far, this is what I've come up with ... which works as far as looping the items is concerned.

$("#domains > option").each(function() {
  if (edit_domain[1] == $(this).val()) {
    //assign this as being the selected value in dropdown
    //exit loop                                   
  }
});

EDIT 1

I changed my code to look like the following:

 //loop through values in drop down to see if we find a match
 $("#domain  option").each(function() {
          var $this=$(this);
          alert($(this).val());
          if ($this.val() == edit_domain[1] ) {
             console.log("compared");
             $("#prefix").val(edit_domain[0]);
             $("#domain").val(edit_domain[1]);
           }
   });

When I refresh my page, I don't get any error messages, but none of the logic inside the loop seems to work. No alerts, no messages in the console. As a test I opened up the debug tools via F12. In the console, I manually typed in the following command:

$("#domain  option").each(function() {
alert($(this).val());
 });

It properly returns all values from my drop down box. I'm not sure where in the code I've gone wrong! Interestingly, when I test in IE, I do see the following error message about the line of HTML code where I declare / create the dropdown box:

HTML1402: Character reference is missing an ending semicolon ";".

And the HTML code looks like this:

<tr><td width="30%">Cell Carrier: (e.g.AT&T)</td><td><select class="form-control" name=domain id=domain></select></td></tr>

Could this be an issue? If my html was bad, I don't think my ajax code that queries the database and then populates the drop down would work at all. But it does... It's just the logic that queries the values in the drop down after it's been populated that's failing.

Happydevdays
  • 1,982
  • 5
  • 31
  • 57
  • possible duplicate of [Javascript loop through ALL HTML select – emerson.marini Apr 20 '15 at 21:52
  • possible duplicate of [for looping on the select options using javascript](http://stackoverflow.com/questions/12257356/for-looping-on-the-select-options-using-javascript) – emerson.marini Apr 20 '15 at 21:53
  • possible duplicate of [Iterate through – emerson.marini Apr 20 '15 at 21:54

3 Answers3

3

When using jQuery, you can use the .val() function for this:

$("#domains").val(edit_domain[1]);
Salman A
  • 262,204
  • 82
  • 430
  • 521
  • salman, my problem was that i needed to do all the logic i 'm discussing the success call back section of an ajax routine. so your answer is correct. sorry for the noise. – Happydevdays Apr 21 '15 at 13:27
  • If the answer does not help then you can unaccept it. Regarding your revised answer (i) `AT&T` should be encoded as `AT&T`, but this is not the main issue (ii) if `$("#domain option").each()` works in console but not on page load then you probably need to wrap your code inside `$(document).ready(...)` – Salman A Apr 21 '15 at 14:55
0

You can also do something like this:

$("#domains option").filter(function() {
    return ($(this).val() == edit_domain[1]); 
}).prop('selected', true);
Tomas Ramirez Sarduy
  • 17,294
  • 8
  • 69
  • 85
0

You can use the below function to compare to option value with the variable.

$(document).ready(function(){
    $('select option').each(function(){
        var $this = $(this);
        if($this.val() == variableValue){
           alert('compared');
           exit;
        }
    });
});