naga4ce
naga4ce

Reputation: 1575

Unable to fetch value : JQUERY

I m creating dynamic rows with two td and text Host-Address is dynamically populated from GET request.

<tr>
   <td class='hostId'>Host-Address</td>
   <td>
    <input id="btnProv" type='button' onClick="enablePro()" class='btn-success' value="Provision">
   </td></tr>

I need to fetch that value using jquery

function enablePro(){
 //var ipAddr = $(this).parent().siblings("td").first().text();
 var row = $(this).closest('tr');
 var ipAddr  = row.find('td.hostId').text();
 alert(ipAddr);
 }

But i get a empty alert box Please enlight my mistake

Upvotes: 3

Views: 136

Answers (2)

Somnath Kharat
Somnath Kharat

Reputation: 3610

You have to pass this or this.id to the function:
this refers to the object of that control while this.id will give you the id of control

onClick="enablePro(this.id)"

function enablePro("#"+sender){
 var row = $(sender).closest('tr');
 var ipAddr  = row.find('td.hostId').text();
 alert(ipAddr);
 }

Upvotes: 0

Sushanth --
Sushanth --

Reputation: 55750

You need to pass this to the function

onClick="enablePro(this)" 

function enablePro(elem){

 var row = $(elem).closest('tr');

this refers to the window object inside the function when you bind events inline.

Bind the event using javascript instead of inline events, then this points to the element that triggered the event.

$('.btn-success').click(enablePro);

Check Fiddle

Upvotes: 5

Related Questions