0

I want to calculate even and odd numbers through a text field,the text field is the range where I put values ,and I select even and odd through select tag.So how am I supposed to get the value from text field to calculate function?

JavaScript Code

 function calculate()
 { 
 var i=0; for(i=1;i<=1000;i++) 
  { 
   if(i%2==0) { 
 document.getElementById("even").value="the numbers is" + i + "<br>";
  } 
else {
document.getElementById("odd").value="the numbers is" + i + "<br>"; }
} 
}

HTML

<body> 
<input type="text" name="value" value="" id="value1" placeholder="Enter the 
first number" /> 
<select> 
   <option value="even" id="even">Even</option> 
   <option value="odd" id="odd">Odd</option>
   </select>
 <p id="value1"> </p> 
<p id="even"> </p>
 <p id="odd"> </p>
 <input type="button" value="Calculate" onclick="calculate()" /> 
6
  • please share your html,javascript code and above all what have you tried? Commented Jun 28, 2018 at 6:30
  • 1
    What have you tried till now ? Commented Jun 28, 2018 at 6:31
  • Use a reference to your text field using DOM selector and use .value function Commented Jun 28, 2018 at 6:31
  • script> function calculate() { var i=0; for(i=1;i<=1000;i++) { if(i%2==0) { document.getElementById("even").value="the numbers is" + i + "<br>"; } else { document.getElementById("odd").value="the numbers is" + i + "<br>"; } } } </script> </head> Commented Jun 28, 2018 at 6:44
  • <body> <input type="text" name="value" value="" id="value1" placeholder="Enter the first number" /> <select> <option value="even" id="even">Even</option> <option value="odd" id="odd">Odd</option> </select> <p id="value1"> </p> <p id="even"> </p> <p id="odd"> </p> <input type="button" value="Calculate" onclick="calculate()" /> Commented Jun 28, 2018 at 6:45

3 Answers 3

1

Refer to this page Input Text value Property - w3schools

To get value from input field you can use :

var x = document.getElementById("myText").value;

To set value in input field you can use :

document.getElementById("myText").value = "my value";
Sign up to request clarification or add additional context in comments.

Comments

0

Setters:

document.getElementById("yourInputField").value = "Value";  // JavaScript
$('#yourInputField').val(Value);  // jQuery

Getters:

var Value = document.getElementById("yourInputField").value;  // JavaScript
var Value = $('#yourInputField').val();  // jQuery

Use them in your calculation method, and enjoy. ;-)

Comments

0

from what I understand you need to pick value of your text box but you will also need a function that gets hit on key press and passes your textbox value to calculate() function

use this function that will get executed once you press a key.

$("#yourtextboxidhere").keyup(function(){

        var textValue =  $.trim( $('#yourtextboxidhere').val() ); 

        // convert the string value from text box to int so you can use it in your calculate function for comparison 
        // as text box values are string)
        var textValue = parseInt(textValue)



    //Put your calculate function here and pass it the value:
    calculate(textValue);

    //calculate function goes here.... (don't forget to add a parameter in you calculate function so you may use it in a correct way)

//replace 'i' in your function with the passed parameter.


    });

don't forget to use jquery.

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.