In my JSP page ,i want to dynamically create a label, which is inside a achor tag. when i clik another anchor tag , i want to get corresponding label text using javascript or jquery.
This is my JSP page code.
<body>
<%
for (int i = 0; i < sam.groupName.length; i++) {
out.print("<a href='#'>");
out.print("<img src='twoMen.jpg'/>");
out.print("<label for='groupsId' id='labelId"+i+"'>"+ sam.groupName[i] + "</label>");
out.print("</a>");
out.print("<a href='javascript:void(0);' onclick='validation(this,"+i+");'>Edit</a>");
}
%>
</body>
Javascript code:
function validation(anchor,i) {
alert("Label value="+????);
}
Quick answer:
Just use the ID of the label you have, then get the text from it.
.text()will return to you the TEXT that is in the tag. Not if that text is contained within another nested tag. If that is the case, use.html()or specify your jQuery selector.UPDATE
I also thought of another solution, this one will be simpler I think:
then you can use the validation function as follows:
This does the same, but it passes the value you need instead of the index pointing to the value you need. This works UNLESS you need the i index for anything else later on.