0

I have a function that needs to be fired on window load only if there on page is no option elements that has "selected" attribute.

<option value="1" selected="selected">Option</option> 

I tried to do this with no luck with next function:

function fireWhenLoad () { if ( $('select option').is(':selected')) { alert("Selected"); } else { alert("Not Selected"); runIfNoSelectedOptions(); } }; $(window).load(function(){ fireWhenLoad(); }); 

The function alerts "Selected" in every case if options has "selected" attribute and if they don't.

So what am I doing wrong and how can I check if option element has "selected" attribute?

Thank you!

3 Answers 3

1

If I understand you correctly, Use $(document).ready() to enure dom elements have been loaded and then loop through the options. You are currently only selecting first option.

$(document).ready(function(){ $("select option").each(function() { var $this = $(this); if ($this.attr("selected") !== undefined) { alert("Selected"); } else { alert("Not Selected"); runIfNoSelectedOptions(); return false; } }); 

});

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

Comments

0

You can use the $.(document).ready() functionality of JQuery to trigger your event.

Comments

0

Have you tried,

$('option').each(function(){ if($(this).is(':selected')){ alert("Selected"); } else { alert("Not Selected"); runIfNoSelectedOptions(); } }); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.