/    Sign up×
Community /Pin to ProfileBookmark

Limit add elements

Hello, how do I add limit? add a maximum of 3 elements. thx for answer

[QUOTE]

<script>

function addEvent() {

var ni = document.getElementById(‘myDiv’);

var numi = document.getElementById(‘theValue’);

var num = (document.getElementById(“theValue”).value -1)+ 2;

numi.value = num;

var divIdName = “my”+num+”Div”;

var newdiv = document.createElement(‘div’);

newdiv.setAttribute(“id”,divIdName);

newdiv.innerHTML = “Element Number ” + num + ” has been added! <a href=”javascript:;” onclick=”removeElement(‘”+divIdName+”‘)”>Remove the element &quot;”+divIdName+”&quot;</a>”;

ni.appendChild(newdiv);

}

function removeElement(divNum) {

var d = document.getElementById(‘myDiv’);

var olddiv = document.getElementById(divNum);

d.removeChild(olddiv);

}

</script>

<input type=”hidden” value=”0″ id=”theValue” />

<p><a href=”javascript:;” onclick=”addEvent();”>P&#345;idat další práci</a></p>

<div id=”myDiv”> </div>

[/QUOTE]

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@FangNov 28.2010 — Count the number of div's in #myDiv
Copy linkTweet thisAlerts:
@Dekker500Nov 28.2010 — Another option, easier to implement though less reliable than counting, is to add a new hidden field, called "theCount"

[CODE]<input type="hidden" value="0" id="theValue"/>
<input type="hidden" value="0" id="theCount"/>[/CODE]


Increment it every time you ADD, decrement every time you REMOVE, and add an IF statement at the beginning of your ADD, such that if the count is > 3, just return (or give error message, whatever)

Dekker (Why Not Fix It?)
×

Success!

Help @gerix 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.15,
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,
)...