What happens when you click currently?
Also make sure you prevent the default action
$(".RatingAjax" + i).click(function(e) {
e.preventDefault();
alert("clicked");
alert(i);
});
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
<div class="p-rating">
<a value ="1" type="button" class="btn RatingAjax1"><i class="fa fa-star-o"></i></a>
<a value="2" type="button" class="btn RatingAjax2"><i class="fa fa-star-o"></i></a>
<a value="3" type="button" class="btn RatingAjax3"><i class="fa fa-star-o"></i></a>
<a value="4" type="button" class="btn RatingAjax4"> <i class="fa fa-star-o"></i></a>
<a value="5" type="button" class="btn RatingAjax5"><i class="fa fa-star-o fa-fade"></i></a></div>
Here is my process i have given different class name for every button. For one start
RatingAjax1upto 5 star
$(document).ready(function() {
var i;
for (i = 1; i <= 5; i++) {
$(".RatingAjax" + i).click(function() {
alert("clicked");
alert(i);
});
}
});
In this
jsfile I want to get the value of<a>tag depending on click event.
How When I click on first button then it value of
ishould be1. If i clicked on5thbutton value ofishould be5.
How to solve this problem?
In html
<div class="p-rating">
<a data-value="1" class="btn RatingAjax1">
<i class="fa fa-star-o"></i>
</a>
<a data-value="2" class="btn RatingAjax2">
<i class="fa fa-star-o"></i>
</a>
<a data-value="3" class="btn RatingAjax3">
<i class="fa fa-star-o"></i>
</a>
<a data-value="4" class="btn RatingAjax4">
<i class="fa fa-star-o"></i>
</a>
<a data-value="5" class="btn RatingAjax5">
<i class="fa fa-star-o fa-fade"></i>
</a>
</div>
In js
$(function() {
$(document).on('click', '.p-rating a', function(e) {
e.preventDefault();
var el = $(this);
var value = el.data('value');
alert(value);
});
});
And then you get exact value.
Please or to participate in this conversation.