views:

77

answers:

3

How can i remove this <p> tag with all its content in javascript?

say i have this html code

<p class="classname">
    <input name="commit" value="Get Value" type="submit"> <span>or Cancel</span>
</p>

Now i need to replace/remove it in javascript, does anyone know how i can remove it?

+6  A: 

Assuming you don't want to change the markup: The easiest way is to use a library. e.g. with jQuery:

jQuery('.classname').remove();

Otherwise, you need to get a reference to the element and then:

el.parentNode.removeChild(el);

Getting a reference to the element is the tricky part. Some browsers implement getElementsByClassName, but you'll have to write your own or use a third party implementation for the rest.

if (!document.getElementsByClassName) {
    document.getElementsByClassName = function (className) {
        /* ... */
    }
}

If you are willing to change the markup, then give the element an id and use document.getElementById to get the reference to it.

David Dorward
Easiest with jQuery **or** any of several other Javascript libraries: http://prototypejs.org, http://code.google.com/closure/library, etc. -- http://en.wikipedia.org/wiki/List_of_JavaScript_libraries
T.J. Crowder
Fair point, I'll confess that I misread the question at first and thought that it said 'jQuery' rather than 'JavaScript'. This required some hurried editing and I didn't tone down the 'use jQuery' enough. The jQuery love that many StackOverflow users have seems to have affected my eyes.
David Dorward
@David: LOL! :-)
T.J. Crowder
+1  A: 

Try this:

<p id="someid">
    <input name="commit" value="Get Value" type="submit"> <span>or Cancel</span>
</p>

function removeElement(id) {
  var d = document.getElementById('myDiv');
  var olddiv = document.getElementById(id);
  d.removeChild(olddiv);
}


removeElement('someid');

With JQuery if you Want

 $('p.classname').remove();
Sarfraz
But i dont have the id for that element
streetparade
@streetparade: If you don't want to use `id` and don't want to use jquery, you should as suggested by @David Dorward.
Sarfraz
+2  A: 

If you have to do it in plain javascript it'll be painful because of Internet Explorer which doesn't support getElementsByClassName() (maybe in newer version ?).

var elems = document.getElementsByTagName('p');
var elemsLenght = elems.lenght;
for (var i = 0; i < elemsLenght; ++i) {
{
  if (elems[i].className == 'classname')
  {
      elems[i].innerHTML = '';
  }
}

But if you can, use a library/framework like jQuery, prototype, dojo, etc..

Boris Guéry
That's an unsafe for in: http://www.jslint.com/lint.html#forin
David Dorward
The class attribute takes a space separated list of classes so `==` isn't good enough for a general getElementsByClassName implementation (although it will suffice in this particular case)
David Dorward
you are right for both comments, the purpose was to explain how to do it in javascript, not to give the code out of the box. The best way to implement getElementsByClassName would have been to prototype it to use as a native javascript function, and check the class name against multiple classes.
Boris Guéry