Question

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.

Was it helpful?

Solution

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);

OTHER TIPS

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();}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top