I've a stars rating sistem, and I want to capture the value selected by user and afterwards send it via AJAX to Server for further processing.
I cant alert the value of the star selected with this code:
$("fieldset input").click(function () {
var radios = document.getElementsByName('rating');
for (var i = 0, length = radios.length; i < length; i++) {
if (radios[i].checked) {
// do whatever you want with the checked radio
var review_stars = radios[i].value;
alert(review_stars);
// only one radio can be logically checked, don't check the rest
break;
}
}
});
But as mentioned, I need to save the value to a variable and afterwards send it via AJAX to the Backend. Therefore, I'm inserting the code above into another function that will execute the AJAX part, like this:
$("#send_review").click(function (e) {
$("fieldset input").click(function () {
var radios = document.getElementsByName('rating');
for (var i = 0, length = radios.length; i < length; i++) {
if (radios[i].checked) {
// do whatever you want with the checked radio
var review_stars = radios[i].value;
// only one radio can be logically checked, don't check the rest
break;
}
}
});
alert(review_stars);
});
But now, I cannot access the review_stars variable. I cannot alert it's value. Why?
UPDATE 1
Updated with answer from Trincot, but value is not alerted: