2

I want to compare the number of items in two select elements(HTML). I tried using length property(with jquery) but it always says that both select element have number of option =1. I am populating both the select elements dynamically. What to do?

      var $options=$("#test_select");
     $options.empty();
     $.each(t_test_list,function(index,value){
    $options.append($("<option>").text(value).attr("value",value));
     });

     var numdate=$("#date_select").length;
var numtest=$("#test_select").length;
alert(numdate);
alert(numtest);
if(numdate!=numtest)
    {
    alert("The number of dates do not match test cases!!!");
    }
1
  • it would be answerable if you post your code. Commented Apr 24, 2014 at 12:35

1 Answer 1

7

try this:

Change:

var numdate=$("#date_select").length;
var numtest=$("#test_select").length;

to:

var numdate=$("#date_select").children('option').length;
var numtest=$("#test_select").children('option').length;

you need to get options length which are the children of select not the select length.

Sign up to request clarification or add additional context in comments.

3 Comments

@RajaprabhuAravindasamy yes you are right. i should not answer it before he post his attempt.
@Awlad Liton It is good to see that you already knew what mistake would have been there :)
Just curious. Is there a reason you are using $("#test_select").children('option') instead of just $('#date_select>option')?

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.