admin管理员组文章数量:1421750
I am building datatables and by setting up total records in database I have total count of them.
Now I am little confused about click event in JavaScript. This is how i am doing this:
for (var cnt = pageno; cnt < pageno + 5; cnt++) {
$("#newList").append('<li id="pageno" value=""' + cnt + ' onclick="GetPageno();" > ' + cnt + '</li>');
}
This is my current loop that will set 5 numbers of list. Now if I want to get value of current list then I am doing this:
function GetPageno() {
//this.val();
alert(document.getElementById('pageno').val());
}
I have 1 to 5 numbers list on web page.
How do I get value of that number I clicked?
Ex. If I clicked "5", then alert should be ing with value "5", same as for all the list elements.
I am building datatables and by setting up total records in database I have total count of them.
Now I am little confused about click event in JavaScript. This is how i am doing this:
for (var cnt = pageno; cnt < pageno + 5; cnt++) {
$("#newList").append('<li id="pageno" value=""' + cnt + ' onclick="GetPageno();" > ' + cnt + '</li>');
}
This is my current loop that will set 5 numbers of list. Now if I want to get value of current list then I am doing this:
function GetPageno() {
//this.val();
alert(document.getElementById('pageno').val());
}
I have 1 to 5 numbers list on web page.
How do I get value of that number I clicked?
Ex. If I clicked "5", then alert should be ing with value "5", same as for all the list elements.
Share Improve this question edited Apr 3, 2013 at 4:55 Fluoxetine 1958 bronze badges asked Apr 3, 2013 at 4:41 ManozManoz 6,61713 gold badges71 silver badges116 bronze badges 2-
$(this).val()
insideGetPageno
should do it. – MD Sayem Ahmed Commented Apr 3, 2013 at 4:42 -
li
shouldn't have value attributes, ids should be unique, you shouldn't use inline javascript, val() a jQuery method ... – Musa Commented Apr 3, 2013 at 4:45
8 Answers
Reset to default 2By doing <li id="pageno"
this you are repeating same id more than once and thats not correct.
Instead use <li id="pageno_'+cnt+'"
and use below code.
$(document).on('click','li[id^="pageno_"]',function(){
alert($(this).attr('value'));
});
DEMO
id's must be unique, you could do:
for (var cnt = pageno; cnt < pageno + 5; cnt++) {
$("#newList").append('<li class="pageno" value="'+cnt+'"> ' + cnt + '</li>');
}
and:
$("#newList").on("click", "li.pageno", function() {
alert( $(this).val() );
});
jsFiddle Demo
There is a small mistake in your value attribute. The double quote to be moved just after the cnt. And Pass the li object to the function i.e, GetPageno(this) as follows,
for (var cnt = 1; cnt <= 5; cnt++) {
$("#newList").append('<li id="pageno" value="' + cnt + '" onclick="GetPageno(this);" > ' + cnt + '</li>');
}
Then get the value of the li object passed in the method.
function GetPageno(obj) {
alert(obj.value);
}
You are calling val()
on li
which is not correct, you can use .attr("value") it would be better if you use data() attributes e.g data-someattributeName to make it more clear. You are assigning same id to more then one elements which is not legal. Assign a class to them and bind event with class and you will be able to get the required attribute.
Add class to li element when you create them, you are not assigning cnt
to value
properly and assigning empty string to value
for (var cnt = pageno; cnt < pageno + 5; cnt++) {
$("#newList").append('<li id="pageno" class="pageno" value=""' + cnt + ' onclick="GetPageno();" > ' + cnt + '</li>');
}
Bind click event using class selector
$(".pageno").click(function() {
alert($(this).attr('value'));
})
Live Demo, using data() and class selector. Here I have also assigned unique Ids
to pageno
.
pageno = 1;
for (var cnt = pageno; cnt < pageno + 5; cnt++) {
$("#newList").append('<li id="pageno"'+cnt+' class="pageno" data-custom="' + cnt + '" onclick="GetPageno();" > ' + cnt + '</li>');
}
$(".pageno").click(function() {
alert($(this).data('custom'));
});
Do like this:
for (var cnt = pageno; cnt < pageno + 5; cnt++) {
$("#newList").append('<li id="pageno"+cnt value=""' + cnt + ' onclick="GetPageno(this.value);" > ' + cnt + '</li>');
}
function GetPageno(val) {
alert(val);
}
you should use class name for all
you can change you code like
for (var cnt = pageno; cnt < pageno + 5; cnt++) {
$("#newList").append('<li class="pageno" value="'+cnt+'"> ' + cnt + '</li>');
}
and add new jquery function in document ready scope
$(".pageno").click(function() {
alert($(this).attr('value'));
})
Id should be unique, you should use class instead. Then you can use jQuery.each() to iterate over your class:
$(".pageno").each(function() {
alert($(this).attr('value'));
})
You're having an issue because of the following:
id
s need to be unique, you should be using aclass
.- You should also attach your event afterwards, not inline (preference).
Refer to your element as
this
and use.text()
not.val()
asvalue
isn't a valid attribute on ali
.for (var cnt = 1; cnt < 5; cnt++) { $("#newList").append('<li class="pageno" ' + cnt + '>' + cnt + '</li>'); } $('.pageno').click(GetPageno); function GetPageno() { alert($(this).text()); }
本文标签: jqueryjavascript Get value by clicking on current value in loopStack Overflow
版权声明:本文标题:jquery - javascript: Get value by clicking on current value in loop - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745323444a2653484.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论