/    Sign up×
Community /Pin to ProfileBookmark

Remove a div by it’s ID ( and all elements within )

<div id=~3~>Industrial Goods<input type=”button” value=”Delete” onClick=”DeleteThis(‘~3~|Industrial Goods’)”><br></div>

So basically… it will delete itself..

Thanks!

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@scragarSep 06.2008 — function removeElement(ele){
if(ele.parentNode &amp;&amp; ele.nodeType){
ele.parentNode.removeChild(ele);
return true;
}else{
return false;// not an element or no parent :(
};
};
Copy linkTweet thisAlerts:
@arron3authorSep 06.2008 — Thanks! That worked well.. to further, I am trying to do this with no luck :

[code=html]
<script Language="JavaScript">

function removeElement(ele){

pieces = ele.split("_");

var ele = pieces[0];

ele.parentNode.removeChild(ele);

}
</script>


<div id="1">Industrial Goods
<input type="button" value="Delete" onClick="removeElement(1_1)">
<br>
</div>
[/code]
Copy linkTweet thisAlerts:
@Declan1991Sep 06.2008 — Probably because you are not passing a string&lt;input type="button" value="Delete" onClick="removeElement([color=red]'[/color]1_1[color=red]'[/color])"&gt;
Copy linkTweet thisAlerts:
@scragarSep 06.2008 — Try this edited version:
function removeElements(){
var ele;
for(var i = arguments[0].length-1; i &gt;= 0; i--){
ele = arguments[i]
if(typeof ele == 'string'){
ele = document.getElementById(ele);
};
try{
ele.parentNode.removeChild(ele);
}catch(e){};
};
};

then call it like this:<i>
</i> &lt;input type="button" value="Delete" onClick="removeElement('1', '2')"&gt;

although it should be noted that elements IDs technicaly shouldn't be allowed to begin with a number.
×

Success!

Help @arron3 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.3,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...