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.

3

3 Answers 3

3

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

$("#domains").val(edit_domain[1]);
Sign up to request clarification or add additional context in comments.

2 Comments

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.
If the answer does not help then you can unaccept it. Regarding your revised answer (i) AT&T should be encoded as AT&amp;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(...)
0

You can also do something like this:

$("#domains option").filter(function() {
    return ($(this).val() == edit_domain[1]); 
}).prop('selected', true);

1 Comment

tom, i also tried your solution, but it doesn't seem to loop through the items. please see my comments in Edit 1.
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;
        }
    });
});

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.