So I have this code:
<body>
<div id="pages"></div>
<script type="text/javascript">
$(function(){
function createDiv(divclass,text) {
$('<div/>', {
class: 'page' + divclass,
html: '<p>' + text + '</p>'
}).appendTo('#pages');
}
for (var i = 1; i <= 4; i++){
createDiv("",i);
}
var pagesdisplay = $('page').length;
console.log(pagesdisplay);
});
</script>
Output is:
<div class="page">
<p>1</p>
</div>
<div class="page">
<p>2</p>
</div>
<div class="page">
<p>3</p>
</div>
<div class="page"><p>4</p></div>
When I look into console it show me 0. How can I count div that are automatically generated by my function?
Edit: I can increment pagedisplay in that for(pagedisplay++) but this is not what I want.
Replace:
with:
You need to prefix the classname with a dot (
.) when using aclass selector. Otherwise$('page')is looking for a<page>element in your DOM which obviously doesn’t exist.