1

Jquery Isn't alerting me when their is no value selected from the Rating form. Would it be because selected="selected" is actually being seen as a value to jquery ?

  <label for="rating">Rating</label>
    <select id="rating" name="rating" />
        <option selected="selected" disabled="disabled">Select a Rating</option>
        <option value="Terrible">Terrible</option>
        <option value="Fair">Fair</option>
        <option value="Ok">Ok</option>
        <option value="Good">Good</option>
        <option value="Excellent">Excellent</option>
       </select>


      $(document).ready(function(){

        // No value for movie_title
        if ($('#movie_title').val() == "" ) {
            alert ("No Film");
        }

        // No Value for actor
        if ($('#leading_name').val() == "") {
            alert ("No actor");
        }

        // No value for rating
        if ($('#rating').val() == "") {
            alert ("No Rating");
        }

        //No value for review
        if ($('#review').val() == "") {
            alert ("No review");
        }

    // Focus on first form field.
        $("input:text:visible:first").focus();

   })
1
  • Oh I'm selecting the wrong id review/rating. Actually that's also wrong... been a long day. Commented Sep 18, 2013 at 17:25

3 Answers 3

2

That's because the default value will be the text if no value is specified.

Give a value of 0 to your default option and check against that:

<option selected="selected" value=0 disabled="disabled">Select a Rating</option>
Sign up to request clarification or add additional context in comments.

2 Comments

just to point out: there are some plugins for jquery (like jquery ui), which needs the value attribute exigent to work
Is this how you would check the value if ($('#rating').val() == 0) { alert ("No Rating"); }
1

$('#rating').val() returns null:

http://jsfiddle.net/KFpFq/

Adding a value attribute doesn't appear to work in Chrome, though checking for null does.

console.log($('#rating').val()); //null
console.log($('#rating').val() == "0"); //false
console.log($('#rating').val() == null); //true

1 Comment

Never knew such a website existed. Wicked
0

You could use the native selectedIndex to find the index of the first selection - https://developer.mozilla.org/en-US/docs/Web/API/HTMLSelectElement

if($('#rating').get(0).selectedIndex === 0){alert("first item")}

2 Comments

It's in the DOM level-1 spec - w3.org/TR/REC-DOM-Level-1/level-one-html.html#ID-94282980 "The ordinal index of the selected option. The value -1 is returned if no element is selected. If multiple options are selected, the index of the first selected option is returned."
(the .get(0) gives you the first raw dom element from jQuery selection - api.jquery.com/get )

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.