How can i save a value in the textbox to a variable

user2969182

I want to store the value given in the text box in a variable. I am beginner to javascript. Please help me out. Here s my code.

<!DOCTYPE html>
<html>
<body>

Days of Journey: <input type="text" id="doj" name="daysofjourney">
<input type="button" value="submit" onclick="dayscounter()">

<script language="javascript" type="text/javascript">
var travel = document.getElementById("doj").value;

function dayscounter() {
    var days;
    for(days = 1; days <= travel; days++) {
        document.write(days);
    }
}
</script>

</body>
</html>  
Zim84

You nearly had it already...

function dayscounter() {
    var travel = document.getElementById("doj").value;
    var days;
    for(days=1;days<=travel;days++)
    {
        document.write(days);
    }
}

The problem was, that your first assignment of the variable travel is made as soon as the HTML code is loaded. The user can't have made an input yet at that time, thus the variable stays empty. If you include document.getElementById("doj").value inside the function, you will get the value at that specific time you launch the function.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how can i save each td value to a variable using curl

From Dev

How can i save previous value of variable in Matlab Function

From Dev

How can i get value from textbox?

From Dev

How can i check a value is pasted in Textbox?

From Dev

How do I save the value of a variable?

From Java

How can I save two variable into a vector?

From Dev

How to save the as I type in a textbox

From Dev

How can I save the value of an instance method?

From Dev

How can I make a textBox value equal to a datagrivew cell value

From Dev

How can I use a textbox value to set a value on a radiobutton

From Dev

How do I store textbox name reference in a Variable to later assign the value of that textbox to another variable

From Dev

How do I store textbox name reference in a Variable to later assign the value of that textbox to another variable

From Dev

How can I save/append text from one textbox to another textbox on the same Form in VB

From Dev

How can I save/append text from one textbox to another textbox on the same Form in VB

From Dev

How can I save my Textbox content in a database? Textbox is programmed in HTML and CSS

From Dev

How can I call a PHP variable within a HTML Textbox Tag?

From Dev

How can I set the value of textbox to "0" if blank or empty?

From Dev

How can I convert textbox value to double in c#?

From Java

How can I sum multiple Numeric textbox value?

From Dev

How can I set the value of textbox to 0 if blank or empty?

From Dev

How can I get textbox value into array and convert into integer in java

From Dev

How can I add value to existing textbox in xaml

From Dev

How can I get button value in textbox of model pop up?

From Dev

Can I save a range to a variable?

From Dev

How do i get the value on the table and save it into the variable

From Dev

How can I use the value of a variable as constant?

From Dev

How can I toggle the value of a variable with a delay?

From Dev

How can I toggle the value of a variable with a delay?

From Dev

How can I use the value of a variable as constant?

Related Related

  1. 1

    how can i save each td value to a variable using curl

  2. 2

    How can i save previous value of variable in Matlab Function

  3. 3

    How can i get value from textbox?

  4. 4

    How can i check a value is pasted in Textbox?

  5. 5

    How do I save the value of a variable?

  6. 6

    How can I save two variable into a vector?

  7. 7

    How to save the as I type in a textbox

  8. 8

    How can I save the value of an instance method?

  9. 9

    How can I make a textBox value equal to a datagrivew cell value

  10. 10

    How can I use a textbox value to set a value on a radiobutton

  11. 11

    How do I store textbox name reference in a Variable to later assign the value of that textbox to another variable

  12. 12

    How do I store textbox name reference in a Variable to later assign the value of that textbox to another variable

  13. 13

    How can I save/append text from one textbox to another textbox on the same Form in VB

  14. 14

    How can I save/append text from one textbox to another textbox on the same Form in VB

  15. 15

    How can I save my Textbox content in a database? Textbox is programmed in HTML and CSS

  16. 16

    How can I call a PHP variable within a HTML Textbox Tag?

  17. 17

    How can I set the value of textbox to "0" if blank or empty?

  18. 18

    How can I convert textbox value to double in c#?

  19. 19

    How can I sum multiple Numeric textbox value?

  20. 20

    How can I set the value of textbox to 0 if blank or empty?

  21. 21

    How can I get textbox value into array and convert into integer in java

  22. 22

    How can I add value to existing textbox in xaml

  23. 23

    How can I get button value in textbox of model pop up?

  24. 24

    Can I save a range to a variable?

  25. 25

    How do i get the value on the table and save it into the variable

  26. 26

    How can I use the value of a variable as constant?

  27. 27

    How can I toggle the value of a variable with a delay?

  28. 28

    How can I toggle the value of a variable with a delay?

  29. 29

    How can I use the value of a variable as constant?

HotTag

Archive