get value from a JS variable and put in an input value

1

I want to get a value from a javascript variable and put it inside a input value to send via get .

My JS looks like this:

$(window).load(function() {
  var count = 10;

  $('a[name=alex]').click(function() {
    document.getElementById("resultado").innerHTML = "" + count + "";
    count += 10;
  });

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script><divid="contador">
  <div class="cont_sub0">
    <h4>Pontuação</h4>
  </div>
  <div class="cont_sub1"><span id="resultado">000</span>
  </div>

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

</div>

I am not able to put the result in value, it is perfect, but in input does not catch.

    
asked by anonymous 03.01.2017 / 04:08

1 answer

2

The innerHTML property is used to write or return the contents of an HTML element, in which case the attribute you want to change is value

document.getElementById("resultado").value = count;

See it working

$(window).load(function() {
  var count = 10;

  $('a[name=alex]').click(function() {
    document.getElementById("span").innerHTML = "" + count + "";
    document.getElementById("input").value = count;
    count += 10;
  });

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script><divid="contador">
  <div class="cont_sub0">
    <h4>Pontuação</h4>
  </div>
  <div class="cont_sub1"><span id="span" name="resultado">000</span>
  </div>

  <input type="text" value="resultado" id="input" name="resultado">
  <a name="alex" href="#">teste</a>
</div>

Other addendums:

  • The id attribute is bound to only one element on each page html ;
  • If you want to work in the same way with elements that are of the same type, you should use document.getElementsByName("name_do_elemento") or even document.getElementsByClassName("name_da_class") which are more generic ways to get multiple elements that have the same name or class, respectively; li>
  • You should also take into account that each element works in a way,% w / o% of type text changes the value to get the expected result, not the content, already elements of type inputs , or div , changes the content, for example.
03.01.2017 / 04:11