0

I'm having such problem. This doesn't work. But I need value="" to be empty before the goDo function is called and be changed after goDo function is called.

<script>
  function goDo(num) {
    if (num == 1) var goID = 10;
    else if (num == 2) var goID = 20;
    var findElement = document.getElementById('1');
    findElement.innerHTML.value=goID;
  }
</script>

<input type="text" value="" id="1">

3 Answers 3

1
<input type="text" value="" id="foobar">

<script>
  function goDo(num) {
    var goID, element;
    if (num === 1 || num === 2){
       goID = num * 10;
    }
    element = document.getElementById('foobar');
    element.value = goID;
  }
</script>
  • innerHTML returns a string and should not be used
  • use === instead of ==
  • IDs in html cannot start with a digit
  • javascript should be in separate file and included right before </body>
Sign up to request clarification or add additional context in comments.

Comments

1

Well, I think there is something wrong with you code. You can try it like this:

  function goDo(num) {
          var goID;
        if (num == 1) {goID = 10;}
        else if (num == 2){ goID = 20;}
        var findElement = document.getElementById('1');

        findElement.value=goID;
  }

goDo(1);

Comments

1

Try

findElement.setAttribute('value', goID);

As an aside, instead of using if statements you can do this to assign goID:

var goID = { 1: 10, 2: 20 }[num];

Here, you are creating a temporary hash map, and then you use num as the key to get the value you want. This is a quick and more idiomatic way of doing what you were trying to do with the if-statements.

1 Comment

I tried with Your 1st code... it's working properly. thank You very much

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.