Removing <p> elements with a certain className

Discussion in 'JavaScript' started by iwasfirst, Dec 10, 2008.

  1. #1
    Hi,

    I'm just getting into the world of Javascript and is faced with this following problem:
    How to filter out and hide/remove only those <p> elements that I've given a certain className with unobtrusive javascript.

    Is there anyone out there that can display an example of how this is done??

    Please - I would be eternally greatful!!
     
    iwasfirst, Dec 10, 2008 IP
  2. lp1051

    lp1051 Well-Known Member

    Messages:
    163
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    108
    #2
    Hi,

    document.getElementsByTagName('p') gives you array of all <p> elements in current document.
    So you go through the array and search for className=='some_class'

    var p_tag=document.getElementsByTagName('p');
    for(var i=0, l=p_tag.length; i<l; i++) { if(p_tag.className=='some_class') do something...(below) }
    p_tag.parentNode.removeChild(p_tag) => remove
    p_tag.style.visibility='hidden' => hide

    Does it help?
    Enjoy JS
     
    lp1051, Dec 12, 2008 IP