How to get the text of label tag using for attribute

zvcxcxv
Kilo Contributor

Hi Everyone,

I need to get the text of <label> tag which has "for" attribute but doesn't have id. There are many other similar nodes, with only the "for" attribute differing.

I have 2 radio buttons, when i click on the radio button, i am fetching the id of the radio button selected. The id of the radio button is same as that of the "for" attribute of <label> tag. So, i want to use the id of radio button and compare it with the label tag and fetch the text.

Here is the example node that contains the label w/ text I'm trying to select.

<div>

<span oclass=" required-marker label_description" style="margin-left: 1px;" assessable_id="891202b24fd9f200beb2e3518110c7c5" id="status.ASMTQUESTION:3d6686fe4fd9f200beb2e3518110c74b" allow_na="false" mandatory="false" class="changed required-marker label_description"></span>

<label for="ASMTQUESTION:3d6686fe4fd9f200beb2e3518110c74b-0" title=" " data-original-title=" ">Text</label>

<p><b>Expected Results</b></p> Result 02

<input name="sys_original.ASMTQUESTION:3d6686fe4fd9f200beb2e3518110c74b" id="sys_original.ASMTQUESTION:3d6686fe4fd9f200beb2e3518110c74b" type="HIDDEN" class="questionSetWidget" value="">

</div>

Any help will be highly appreciated!!!

Thanks in advance!!

1 ACCEPTED SOLUTION

Arnoud Kooi
ServiceNow Employee
ServiceNow Employee

Try something like:



var myId = 'ASMTQUESTION:3d6686fe4fd9f200beb2e3518110c74b-0'; //your radiobutton id


var myLabel = $j("label[for='"+myId+"']").text();


View solution in original post

5 REPLIES 5

Arnoud Kooi
ServiceNow Employee
ServiceNow Employee

Try something like:



var myId = 'ASMTQUESTION:3d6686fe4fd9f200beb2e3518110c74b-0'; //your radiobutton id


var myLabel = $j("label[for='"+myId+"']").text();


Hi Arnoud,



Thank you for the solution It worked perfectly.


I noticed that i have one more <label> tag with same for value which is displayed below the above mentioned lines of codes.



Here is the full code:


<div>


<span oclass=" required-marker label_description" style="margin-left: 1px;" assessable_id="891202b24fd9f200beb2e3518110c7c5" id="status.ASMTQUESTION:3d6686fe4fd9f200beb2e3518110c74b" allow_na="false" mandatory="false" class="changed required-marker label_description"></span>


<label for="ASMTQUESTION:3d6686fe4fd9f200beb2e3518110c74b-0" title=" " data-original-title=" ">Text</label>


<p><b>Expected Results</b></p> Result 02


<input name="sys_original.ASMTQUESTION:3d6686fe4fd9f200beb2e3518110c74b" id="sys_original.ASMTQUESTION:3d6686fe4fd9f200beb2e3518110c74b" type="HIDDEN" class="questionSetWidget" value="">


</div>


<td class="asmt_definition">


<label for="ASMTQUESTION:3d6686fe4fd9f200beb2e3518110c74b-0" class="hiddenRadioLabel">Failed</label>


<input onclick="if (typeof(variableOnChange) == 'function') variableOnChange('ASMTQUESTION:3d6686fe4fd9f200beb2e3518110c74b'); showDependentQuestions(this); showDependentQuestions(this);" name="ASMTQUESTION:3d6686fe4fd9f200beb2e3518110c74b" id="ASMTQUESTION:3d6686fe4fd9f200beb2e3518110c74b-0" type="radio" title="" class="questionSetWidget" value="0" data-original-title="Failed"></td>



Could you please tell me how to get the text of the first label tag and ignore the second label tag? Here i just want to get "Text" and ignore the "Failed" text.


Arnoud Kooi
ServiceNow Employee
ServiceNow Employee

This are jQuery selectors, not ServiceNow, there are a lot of resources when you google.



A solution is to add the :first selector


$j("label[for='"+myId+"']:first").text()



Looking at your code, alternative you could exclude the ones that have the hiddenRadioLabel class in the following way|:


$j("label[for='"+myId+"']:not(.hiddenRadioLabel)").text()



Can you mark the first answer as correct, thanks!


Hi Arnoud,



Thank you!! It is working now. I am new to jQuery and I tried with different codes but it didn't get me anywhere.