1

I'm new to javascript and I'm trying to build a script within html that creates a variable x, and then writes it to a cell in a table. This is what I've come up with, but it doesn't work.

<table border="1">
    <tr>
        <td>Text</td>
        <td id="xx"></td>
        <td><button type="button" onclick="x()">Check</button></td>
    </tr>
</table>

<script>
    function x() {
        var x = idec.device_read("D", 0901, 0, 0);
    }
    document.getElementById("xx").innerHTML = x;
</script>

The idea is that when you click the button, the result from var x can be viewed in the cell. But as it didn't work, I added the following to the script

var y = 5;

right at the top

Then I added

<p>
    <script>
        document.write(x + "<br>");
        document.write(y);
    </script>
</p>

Which in theory should of at least added a five after the table... or not? Because no five appeared...

Part of the problem could be that I've got the

var x = idec.device_read("D", 0901, 0, 0);

part wrong as I've no way of properly verifying that.

3
  • 3
    firstly, don't make your function names the same as variable names since function names are practically variables. So don't have function x() , and then a var x = Commented Jan 9, 2014 at 14:21
  • Good advice, though not the cause of the problem in this case, since variable x is defined within the scope of function x... but it looks like that's more luck than wisdom in this case. Commented Jan 9, 2014 at 14:25
  • I just put x when typing it onto here - they have other names... Commented Jan 9, 2014 at 14:25

2 Answers 2

1

You're doing writing the content to the table cell outside of your function, making it run immediately upon page load. At that time variable x is not yet declared, so it won't do anything. Then when clicking the button, all that happens is that a variable x is filled, but nothing is done with that.

function x() {
    var x = idec.device_read("D", 0901, 0, 0);
    document.getElementById("xx").innerHTML = x;
}

I don't know what idec.device_read() is supposed to do, so I can't comment on whether or not that is part of the problem. Still, I'd recommend breaking up the problem in smaller parts by replacing that function call with a fixed value. If you can get the writing to the table cell working, you can then add back that code to solve the issue of reading values from the device. Divide and conquer:

function x() {
    var x = 'test';
    document.getElementById("xx").innerHTML = x;
}

As pointed out in the comments by AlienArrays: naming your functions in variables the same is a dangerous thing to do in Javascript:

function writeDeviceValueToCell() {
    var deviceValue = 'test';
    document.getElementById("xx").innerHTML = deviceValue;
}

When posting questions related to client-side development, please consider adding your code to a JS Fiddle. That way, people interested in helping out can more easily see what you're trying to do. I've created a fiddle for this problem to demonstrate: http://jsfiddle.net/5gESD/

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

3 Comments

So how can I change the "fixed value" you specified to the idec one?
Reverse what I did, paste the idec.device_read() call back over the 'test' value that I suggested.
So I use your third set of code, and replace test with my code? With or without the ''?
0

try this

function setX()
{
var x='10';
 document.getElementById("xx").innerHTML=x;
}

<table border="1">
<tr>
<td>Text</td>
<td id="xx"></td>
<td><button type="button" onclick="setX()">Check</button></td>
</tr>
</table>

variable x is scoped inside the function x so when you set the innerHTML the scope of x is function x

so first change the function name and set the the value inside the function.

here is the sample bin link http://jsbin.com/ANUBeXox/1/

1 Comment

Actually, the variable is declared within the function, so they're not within the same scope...

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.