-3

I have the following HTML, how can I retrieve the ID of the input from the tag's name in JavaScript?

<div class="named-tags">
     <label for="radio_01" class="radio" >
         <input type="radio" name="Name[labeled_tags][]" value="01" id="radio_button_01">
        Fblabel
         <span class="control-indicator">  </span>
         </label>
        </div>
Ari Seyhun
  • 11,506
  • 16
  • 62
  • 109
Paul
  • 1
  • 2
  • Please further clarify what you are trying to achieve. – doubleOrt Dec 06 '17 at 03:08
  • I am trying to get the id of radiobutton using the label. The radio buttons are created dynamically and we won't know the id of the radiobutton . So we need to do this method. I found help to get label associated an element using Id but cannot find id using a label. I have number of radio button which are created like this . My aim is to select the radio button according to a utm tag value. Further more this radiobutton are hidden so it cannot be selected by user. – Paul Dec 06 '17 at 06:41

1 Answers1

0

Using document.getElementsByName('Name[labeled_tags][]') will return an array of elements with the specified tag.

You're most likely wanting to select the very first element with the tag name, in this case you would use document.getElementsByName('Name[labeled_tags][]')[0]

let inputElem = document.getElementsByName('Name[labeled_tags][]')[0];
console.log(inputElem.outerHTML);
<div class="named-tags">
 <label for="radio_01" class="radio">
  <input type="radio" name="Name[labeled_tags][]" value="01" id="radio_button_01"> Fblabel
  <span class="control-indicator"> </span>
 </label>
</div>
Ari Seyhun
  • 11,506
  • 16
  • 62
  • 109