how do I indicate a javascript generated ID using javascript/jQuery?
I'm counting the number of p tags in a certain div with individual IDs where the p tags and the IDs are randomly generated by php and in varying numbers but in a patterned ID (otherUser1, otherUser2, otherUser3, etc.)
for (var i = 1; i <= numUsers; i++) {
var tagId = '#otherUser' + i + '';
document.write(tagId);
var userName = $(tagId).html();
document.write(userName);
}
whe开发者_如何学Pythonn I document write the userName variable above, it gives me null because just inserting the variable tagId in the jQuery brackets doesn't do the job. How do I do this properly? - I don't mind either using javascript or jQuery, either is fine.
I have tried this using jQuery. This is very well working with my localhost. But, still I don't this will solve your issue or not.
I have implemented javaScript as :
function submit(){
var numUsers=3;
for (var i = 1; i <= numUsers; i++) {
var tagId = '#otherUser' + i;
var userName = $(tagId).text().toString();
$('#user').append("Username" + i + " : " + userName + "<br/>");
}
}
And that your HTML is :
<div id="otherUser1">userName1</div>
<div id="otherUser2">userName2</div>
<div id="otherUser3">userName3</div>
<input type="button" value="Click Me!!" onclick="submit();"/>
<div id="user"></div>
This will display the DIVs currently. But, in place of this HTML, there would be your PHP auto-generated DIVs, that will not be displayed later.
I have also created this fiddle. I don't know why this is not working even if being the same !!!
Please correct me if I am wrong anywhere.
Thanking You.
EDIT : perhaps the jsfiddle not supporting the functions, (so removed function line from javaScript) ! So, updated the fiddle & that is working properly.
精彩评论