0

Why doesn't surveyName's value change?

<script type = 'text/javascript'>
    surveyName = "catNull";
    function test(){
        window['surveyName'] = "catTest";
    }
</script>

</head>
<body>

    <input onclick = 'test()' id = 'cat' class = 'test' type = 'button' value = 'category' />

<script>
document.write('<input id = "survey" class = "test" type = "button" value = "'+surveyName+'" />');
</script>
4
  • It won't change until you click the button. Commented Jul 22, 2011 at 15:09
  • 3
    If a variable changes in a webpage, but no-one is there to see it, did it really change? Commented Jul 22, 2011 at 15:10
  • SLaks, you mean, JavaScript version of Shroedinger's cat? :) Commented Jul 22, 2011 at 15:11
  • What will happen here is a button will be written with a value "catNull", when you click it surveyName the variable changes, however that doesn't effect the input button's value. For that you would need something like document.getElementById('survey').value = "catTest";. JavaScript variables are not tied into the DOM automatically this way. Commented Jul 22, 2011 at 15:13

4 Answers 4

1

It does change.

However, you never observe the change.

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

Comments

0

Because you don't change input object's value, only the variable.

The element is independent of the variable. If you want to change what the browser shows, you must do:

document.getElementById("...").value = window.surveyName;

1 Comment

Welcome. Here on stack overflow, you say "thank you" by clicking the UP arrow. Or the green check mark for a BIG thank you :)
0

Because you did not click on the input yet.

Comments

0

It does. But you've already output the variable to the page, and what you've output doesn't change.

If you want to change the input's value:

document.getElementById("survey").value = "new value";

That works because you've assigned the element the id value "survey", so you can retrieve the element via document.getElementById passing in that id, and use the element's value property to set the value.

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.