loomie
loomie

Reputation: 616

jQuery select value undefined

Ok, I have a really simple problem that is definetly easy to solve, but I can't find the solution. I have a function that get's called when I press the button. This function should do something, but firstly alert the values I typed in. I got a textarea for reviewText and a select for review

Now, I want to alert those values, as soon as I press the button

echo '<select name="rating" size="1">';
for($i = 1; $i < 6; $i++)
{
    if($i == 1)
        echo '<option value="' . $i . ' Star" name="' . $i . ' Star" id="' . $i . ' Star">' . $i . ' Star' . '</option>';
    else
        echo '<option value="' . $i . ' Stars" name="' . $i . ' Stars" id="' . $i . ' Stars">' . $i . ' Stars' . '</option>';   
}
echo '</select>';

The problem now comes in the JavaScript-function. So far I have this

function addReview(itemid, userid)
{
    var reviewText = $('#reviewText').val();
    var rating = $('#rating').val();

    alert(reviewText);
    alert(rating);
}

It shows me the value of the textarea, but not of the select. Why? What am I doing wrong? I also tried to delete all values, ids and names from the options but that didn't help either. What is the problem?

Upvotes: 1

Views: 3010

Answers (1)

zerkms
zerkms

Reputation: 255105

Because #rating selector selects an element with id="rating" not name="rating"

Put id="rating" additionally to your select tag (preferred), or select it as select[name="rating"] (not recommended but will work as well).

Upvotes: 6

Related Questions