Reputation: 965
I have to make a button which has a value that is a javascript variable
<script>
for(i=0; i < count; i++)
{
var row = table.insertRow(table.rows.length);
var cell = row.insertCell(0);
cell.innerHTML = '<input type="submit" value=i onClick="someFunc(this.value)" />';
}
</script>
But when i open the page the value is just i. How do set the value of i in the button.
Upvotes: 0
Views: 136
Reputation: 4620
if you assign i into quotes its take as a string. Not a variable. So u can use '+i+'
.
+ Concatenation Operator
<script>
for(i=0; i < count; i++)
{
var row = table.insertRow(table.rows.length);
var cell = row.insertCell(0);
cell.innerHTML = '<input type="submit" value='+i+' onClick="someFunc(this.value)" />';
}
</script>
Upvotes: 0
Reputation: 3820
Here you are setting the value of button by wrong method..
<script>
for(i=0; i < count; i++)
{
var row = table.insertRow(table.rows.length);
var cell = row.insertCell(0);
cell.innerHTML = '<input type="submit" value='+i+' onClick="someFunc(this.value)" />';
}
Upvotes: 0
Reputation: 3837
Wouldn't it just be
cell.innerHTML = '<input type="submit" value="'+ i + '" onClick="someFunc(this.value)" />';
so you use the value of i instead of the string i?
Upvotes: 4