0

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.

3 Answers 3

4

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?

Sign up to request clarification or add additional context in comments.

Comments

0

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)" />';
    }

Comments

0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.