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

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

jenny32
Tera Guru

Hi Everyone,



I am facing similar issue.



Here is my code:


<!-- first tr   -->


<tr class="1603068b0f65320020af06ace1050e15--tm_test_plan--75910e8b0fa1320020af06ace1050e6c tm_test_plan--75910e8b0fa1320020af06ace1050e6c--controlled" id="CATEGORY_LABEL">  


      <td>


              <table class="indentOne" style="width: 100%">


                      <tbody>


                              <tr>


                                      <td>


                                              <div class="question_category_label less_margin" id="assessable-label-">Quote for "pick to order" part, convert to order and invoice</div>


                                              <div class="category_description 1603068b0f65320020af06ace1050e15--tm_test_plan--75910e8b0fa1320020af06ace1050e6c"></div>


                                      </td>


                              </tr>


                      </tbody>


              </table>


      </td>


</tr>


<!-- second tr   -->


<tr class="1603068b0f65320020af06ace1050e15--tm_test_plan--75910e8b0fa1320020af06ace1050e6c tm_test_plan--75910e8b0fa1320020af06ace1050e6c template_group_row" style="">  


    <td class="question_label" id="label_ASMTQUESTION:4b03068b0f65320020af06ace1050e27">


    <div><span oclass="" style="margin-left: 1px;" assessable_id="75910e8b0fa1320020af06ace1050e6c" id="status.ASMTQUESTION:4b03068b0f65320020af06ace1050e27" allow_na="false" mandatory="false" class=" required-marker label_description"></span>


<label for="ASMTQUESTION:4b03068b0f65320020af06ace1050e27-0" title="" data-original-title="">Approve Quote and convert to Order</label>


      </div>


      </td>


</tr>


Please note that we have multiple similar nodes like "second <tr>".



Here i am fetching the <label> text in the "second <tr>" and i am getting second <tr> class name by using the below code:


var myId= //some ID


var className=$j("label[for='"+myId+"']:not(.hiddenRadioLabel)").parents().eq(14).prop('className');



Now i want to fetch the first class name, so i used the below code:


var nextClassName=$j('"+className+"').closest("tr").prop('className');   //but i didnt get output for this and i cant use "closest("tr") if i retrive class name from 3rd or 4th <tr> tag.


Using "nextClassName" i have to fetch the highlighted <div> text i.e., i have to fetch "Quote for "pick to order" part, convert to order and invoice" as my final output.


Since i didn't get any value for "nextClassName" i am not able to fetch the <div> tag text in the "first <tr>".



Could anyone please correct me where i am going wrong



Best Regards,


Jennifer