0

So I have a script block:

<script>
var totalPopulation = 0;

for(xxxxx){
  totalPopulation = totalPopulation + xxx
}
</script>


<tr>
  <input type="text" name="censusPop" value=totalPopulation/>
<tr>

I'm still quite green to javascript. But is there a way to assign the value of a variable in a script block to a HTML element like input type? I know that the code is unreachable.

0

4 Answers 4

0

hope it will help you to understand how javascript work

<html>
<head>
<script>
    var totalPopulation = 0;

    function addAndShowPopulation(population) {
         totalPopulation = totalPopulation + population;

         var input_tag = getTag('my_input_id');
         input_tag.value = totalPopulation;
    }

    function startAdding() {
         addAndShowPopulation(10);

         setTimeout( function(){startAdding();},1000);
    }

    function getTag(id) {
       return document.getElementById(id);
    }

</script>
</head>
<body onload="startAdding();">

<div>
     <input type="text" id="my_input_id" name="censusPop" value="" placeholder="Total Population"/>

</div>
</body>
</html>
Sign up to request clarification or add additional context in comments.

Comments

0

Yes, you just have to give an id to the input, for instance "id = my_input_id"; Then, in the javascript, just write :

$("my_input_id").value=totalPopulation;

That's how ajax works: find html elements ids and fill them dinamically using javascript values.

Just be carefull that the html in read before the JS. If not, $("my_input_id") will return Null

2 Comments

Need more than just JavaScript, need jQuery.
You cannot apply a value on a jQuery object like this! Try $("my_input_id").val(totalPopulation);
0

More so, you need to do something like this:

<tr>
 <td>
  <input type="text" id="censusPop" name="censusPop" value=""/>
 <td>
<tr>
<!-- Other stuff -->
<script>
var totalPopulation = 10;
// or for loop, or whatever here.
var censusText = document.getElementById('censusPop');
censusText.value = totalPopulation;
</script>
</body>

HTML and JavaScript can interact, but not directly like that. The best thing to do is use <script> tags to setup code that updates the browser DOM. By putting the <script> tags after the HTML, usually at the bottom of the <body> tag, you allow the browser a chance to create the elements before you actually try to use them.

Another note: <tr> tags should contain <td> tags, it's the difference between a row and a column.

Comments

0

If you need to do multiple DOM manipulations, I'd suggest using jQuery is the proper way.

<tr>
    <input id="censusPop" type="text" name"censusPop" value=""/>
</tr>
<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script>
    // make sure, the code is executed when the DOM is ready
    $(function () {
        // grab the input element by its ID
        var $input = $('#censusPop'),
            totalPopulation = 0;
        // do your population calculations
        totalPopulation = ....;
        // assign the value to the input element
        $input.val(totalPopulation);
    });
</script>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.