How to get the label value from table row when row number is known, using Javascript

StackOverflow https://stackoverflow.com/questions/23514885

  •  17-07-2023
  •  | 
  •  

سؤال

I have a table like this-

<table>
 <tr>
   <td>
      <label id="lbl1" value="1">Label1</label>
   <td> 
   <td>
     Some data
   </td>
   <td>
     Some data
   </td>
 </tr>
 <tr>
   <td>
      <label id="lbl2" value="1">Label1</label>
   <td> 
   <td>
     Some data
   </td>
   <td>
     Some data
   </td>
 </tr>
 <tr>
   <td>
      <label id="lbl3" value="1">Label1</label>
   <td> 
   <td>
     Some data
   </td>
   <td>
     Some data
   </td>
 </tr>
</table>

My problem is that I want to alert the value of label present in the second row's first column. Assume that I don't know label id means I know its pattern like lbl1,lbl2 or lbl3.. but not exactly what it is in the second row.

هل كانت مفيدة؟

المحلول

If you are okay to use jQuery use this fiddle

var label = $('table tr:eq(1) td:eq(0)').find("label").attr("value")
alert(label);

نصائح أخرى

You can use something like next

var labels = document.getElementsByTagName("label");
for (var i=0; i<labels.length; i++)
   if (labels[i].id && labels[i].id.indexOf("lbl") == 0){
      //you have found the label in the first row
   }

You Can get label value by class name

$("label[class=lblclass]").each(function() {var result= $(this).val(); });

(OR) You can get the Particular Label Value by ID

function getlabel_value(){var result=$('#lbl1').val();}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top