- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-16-2017 12:01 AM
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!!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-16-2017 03:17 AM
Try something like:
var myId = 'ASMTQUESTION:3d6686fe4fd9f200beb2e3518110c74b-0'; //your radiobutton id
var myLabel = $j("label[for='"+myId+"']").text();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-16-2017 03:17 AM
Try something like:
var myId = 'ASMTQUESTION:3d6686fe4fd9f200beb2e3518110c74b-0'; //your radiobutton id
var myLabel = $j("label[for='"+myId+"']").text();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-16-2017 11:00 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-17-2017 01:54 AM
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-17-2017 02:29 AM
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.