/    Sign up×
Community /Pin to ProfileBookmark

how many elements???

[code]
<script type=”text/javascript”>
function addElement() {
var ni = document.getElementById(‘myDiv’);
var numi = document.getElementById(‘theValue’);
var num = (document.getElementById(‘theValue’).value -1)+ 2;
numi.value = num;
var newdiv = document.createElement(‘div’);
var divIdName = ‘my’+num+’Div’;
newdiv.setAttribute(‘id’,divIdName);
newdiv.innerHTML = ‘Element Number ‘+num+’ has been added!<a href=’#’ onclick=’removeElement(“‘+divIdName+'”)’>Remove the div “‘+divIdName+'”</a>’;
ni.appendChild(newdiv);
}

function removeElement(divNum) {
var d = document.getElementById(‘myDiv’);
var olddiv = document.getElementById(divNum);
d.removeChild(olddiv);
}
</script>

<li recordid=”3″><input type=”hidden” value=”0″ id=”theValue” />
<p><a href=”javascript:;” onclick=”addElement();”>Add Some Elements</a></p>
<div id=”myDiv”> </div>
</li>
[/code]

my code is the above..is there any way to get how many element i have created?

thnx you very much in advance

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@simonzackDec 17.2008 — Implement some counter function,

or use document.evaluate("//*",document,null,7,null).snapshotLength();

and compare before/after
Copy linkTweet thisAlerts:
@toicontienDec 17.2008 — [CODE]function countElements() {
return document.getElementById("myDiv").childNodes.length;
}[/CODE]

Make sure your DIV tag has absolutely nothing inside of it, even white space characters:
[CODE]<div id="myDiv"></div>[/CODE]
×

Success!

Help @d_psim spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.30,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...