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
  • 44,692
  • 7
  • 66
  • 118
user3564050
  • 138
  • 1
  • 3
  • 15
  • 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,553
  • 8
  • 36
  • 62
  • @user3564050 I'm glad that I could help you. +1. – Vedant Terkar May 20 '14 at 09:09
  • Well using `""` or `''` for concatenation and to make passing arguments as strings is useless. – Rahil Wazir May 20 '14 at 09:18
  • @RahilWazir Then How will you pass arguments? May I know that? – Vedant Terkar May 20 '14 at 09:20
  • 1
    @VedantTerkar You are passing the arguments correctly. What I mean is Just use your parameter in your function definition like this `elem.getAttribute(atr)`. Using `""` or `''` either side is just an empty string it doesn't make passed parameters as string. – Rahil Wazir May 20 '14 at 09:24