Olivia
Olivia

Reputation: 2171

Passing Parameters - Dynamic HTML with JavaScript Function

I have a javascript function that dynamically builds a table. I have a function that gets called once a button inside of that table is clicked.

var bankButtonRefresh = '<a href="javascript:void(0)" onClick="refreshBankDataFunction('+parentId+')" 
class="btn-sm btn-primary permissionDeny pull-right" role="button">Refresh</a>';
row +='<td style="position:relative">' + bankButtonRefresh2 + '</td>';

When I printed out the value of bankButtonRefresh2 the value is:

<a href="javascript:void(0)" onClick="refreshBankDataFunction(a34c0000000jiecccA)" 
class="btn-sm btn-primary permissionDeny pull-right" role="button">Refresh</a>

So I can see that the parentId is passing incorrectly.

I am not trying to do anything fancy, I am just trying to get the Id passed to the function but I am getting no response from the button when I click on it.

function refreshBankDataFunction(obj){ //obj is the parentId
      alert('got into the function: ' + obj);
}

Am I not passing the parentId correctly?

Upvotes: 0

Views: 3495

Answers (2)

Adrianopolis
Adrianopolis

Reputation: 1292

Where you are building your link add an extra set of wrapping single quotes such as: onClick="refreshBankDataFunction(''+parentId+'')"

Upvotes: 0

Tom el Safadi
Tom el Safadi

Reputation: 6776

Try this:

var bankButtonRefresh = '<a href="javascript:void(0)" onClick="refreshBankDataFunction({param:'+parentId+'})" class="btn-sm btn-primary permissionDeny pull-right" role="button">Refresh</a>';


function refreshBankDataFunction(obj){ //obj is the parentId
      alert('got into the function: ' + obj.data.param);
}

Upvotes: 1

Related Questions