Reputation: 23
I try to pass a value from onclick
button, but I cant get the value and it shows the error as :
index.html:1 Uncaught ReferenceError: wims is not defined at HTMLButtonElement.onclick (index.html:1).
Please check my code below :
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
{...........
var a = "wims";
$(this).append('<tr><th><button type="button" id="'+item[0]+'" OnClick="nextpage('+a+')">'+item[0]+'</button></th><th>$'+item[11]+'</th></tr>');
.........
}
nextpage = function (id){
var a= id;
alert(a);
}
Upvotes: 1
Views: 175
Reputation: 1326
try this:
$(this).append('<tr><th><button type="button" id="'+item[0]+'" OnClick="nextpage(\''+a+'\')">'+item[0]+'</button></th><th>$'+item[11]+'</th></tr>');
else javascript
searchs for variable
with name wims
instead of a string with this value.
Upvotes: 2
Reputation: 321
When you create the table row, You are printing the value of a variable in string("wims") and then passing this in nextpage function. Just remove the single quotes and plus signs.
$(this).append(
'<tr><th><button type="button" id="'+item[0]+'" OnClick="nextpage(a)">'+item[0]+'</button></th><th>$'+item[11]+'</th></tr>'
);
// Just call nextpage with a variable without single quotes and plus sign.
// OnClick="nextpage(a)" Like this.
I have tested this.
document.write('<tr><th><button type="button" id="item0" OnClick="alert(a)">item0</button></th><th>$item0</th></tr>');
Type this code in any console of browser it works. Thanks.
Upvotes: 0