1

I've got a label for an input. Defined like:

<label for"idofparentelement">innerHTML</label>

Found nothing where the label for hasn't got an id.

How can i remove it with JavaScript without giving an id.

Deduplicator
  • 43,322
  • 6
  • 62
  • 109
user3564050
  • 138
  • 1
  • 3
  • 14
  • What do you want to do? its sounds so simple that my mind tries to debate there something behind it. – Viscocent May 20 '14 at 08:56
  • I've got an element. An Input. This input has an ID. So i've got a label too. the label hasn't got an id. How can i remove the label for my input element ? – user3564050 May 20 '14 at 08:57
  • @user3564050 — So your question is "How can I find an element in the DOM based on the value of its `for` attribute?"? – Quentin May 20 '14 at 08:58
  • Duplicate: [Find an element in DOM based on an attribute value](http://stackoverflow.com/questions/2694640/find-an-element-in-dom-based-on-an-attribute-value) – Quentin May 20 '14 at 08:59
  • doesn't work for me ^^ – user3564050 May 20 '14 at 09:03
  • @user3564050 — It should work for you. I've no way to tell what is wrong with your implementation of it. – Quentin May 20 '14 at 09:21

1 Answers1

1

To remove element with specific attribute Use this function:

function removeElem(tag,atr,vl)
{
    var els = document.getElementsByTagName(tag);
    vl=vl.toLowercase();
    for (var i = 0; i<els.length; i++) {
    var elem=els[i];
    if(elem.getAttribute(atr)){
    if ( elem.getAttribute(atr).toString().toLowercase()==vl){
    elem.remove();
    return;
    }
    }
    }
}

and First of all Change your html like:

<label for="idofparentelement">innerHTML</label>

Now for your case Use this as: removeElem('label','for','idofparentelement');

Here is the working:

Fiddle

Hope it'll help you cheers :)!!

Vedant Terkar
  • 4,365
  • 5
  • 35
  • 60