0

I have 3 inputs like this :

<input type="text" id ="text1"/><br>
<input type="text" id ="text2"/><br>
<input type="text" readonly="readonly" disabled="disabled" id ="text3"/>

the 3rd input is disabled and I like that his value will equal to : (val(text2)/val(text1)*100-100) whene I pass the value of the two other input

so for that I try this script :

var p1 = document.getElementById('#text1').value;
var p2 = document.getElementById('#text2').value; 
document.getElementById('#text3').value = ((p1/p2)*100)-100;

but does not work , someone can help me please ?

4
  • 4
    Open your browser console, you will find errors displaying in there. document.getElementById('#text1') is incorrect. You don't need to have # in the selectors. document.getElementById('text1') Commented Aug 7, 2017 at 12:38
  • thanx but how can I get this value instantaneous whene I pass the two values of the other input ? Commented Aug 7, 2017 at 12:42
  • Use an event listener... onchange or oninput would work just fine. Commented Aug 7, 2017 at 12:44
  • Provided with the Solution with the help of Jquery Event Handlers. Have a Check of it. Commented Aug 7, 2017 at 12:56

5 Answers 5

6

The # exists in jquery. you should type pure javascript :

var p1 = document.getElementById('text1').value;
var p2 = document.getElementById('text2').value; 
document.getElementById('text3').value = ((p1/p2)*100)-100;
Sign up to request clarification or add additional context in comments.

7 Comments

Actually the # comes from CSS, jQuery just makes use of it. Both use it to identify the ID of an element. ¯\_(ツ)_/¯
How can I get it instantaneous just whene I pass the values of input1 and input 2 ?
@JayBlanchard # can also be used in querySelector() so it isn't just CSS or jQuery
@DhiaEddineFarah you should put it on change event of inputs.
Yep @NewToJS, which is what jQuery does under the hood. I never excluded VanillaJS from using it ;)
|
1

Remove the '#' they should not be here. The '#' is for JQuery not raw JS.

var p1 = document.getElementById('text1').value;
var p2 = document.getElementById('text2').value; 
document.getElementById('text3').value = ((p1/p2)*100)-100;

Or if you are using JQuery :

var p1 = $('#text1').val();
var p2 = $('#text').val();
$('#text3').val( ((p1/p2)*100)-100);

Comments

0

Or use JQuery as you have this tag (shorter version):

var p1 = $('#text1').value;
var p2 = $('#text2').value; 
if(p2 != 0)
{
    $('#text3').value = ((p1/p2)*100)-100;
}

Don't forget to check the denominator for 0.

8 Comments

What if the OP hasn't included jQuery in their project?
@JayBlanchard If the OP hasn't included the jQuery library to their project or know how to do so then it shouldn't be tagged in the question as the OP would have no intentions of using it.
@JayBlanchard In this case I wouldn't add the answer. But he tagged as JQuery.
You would think that @NewToJS, but I have seen it over and over again.
@JayBlanchard Let's wait for the feedback from the OP.
|
0

Explanation: You have used document.getElementbyID to retrieve the value from the text box and the method what you have adopted is wrong.

Method One:

var p1 = document.getElementById('text1').value;
var p2 = document.getElementById('text2').value; 
document.getElementById('text3').value = ((p1/p2)*100)-100; 

Another Method you can take is by

var p1 = $('#text1').val();
var p2 = $('#text2').val; 
var p3 = ((p1/p2)*100)-100;
$('#text3').val(p3);

Getting the Value Instantaneously from the Inputs that has been given

HTML:

<input type="text" id ="text1" onblur="someFunction()"/><br>
<input type="text" id ="text2" onblur="someFunction()"/><br>
<input type="text" readonly="readonly" disabled="disabled" id ="text3"/>

Javascript / Jquery:

function someFunction()
{
    var p1 = $('#text1').val();
    var p2 = $('#text2').val; 
    if(p1=='' || p2=='')
    {
        alert('Please Fill all the Values');
    }
    else
    {
        var p3 = ((p1/p2)*100)-100;
        $('#text3').val(p3);
    }    
}

4 Comments

How can I get it instantaneous just whene I pass the values of input1 and input 2 ?
You can use the onchange() or onblur() jquery events to calculate the process instantaneously
how can I please do that ?
I have made the code in my solution. Have a try of that.
0

replace your code with the three lines,

var p1 = parseFloat(document.getElementById('text1').value);
var p2 = parseFloat(document.getElementById('text2').value); 
document.getElementById('#text3').value = parseFloat(((p1/p2)*100)-100);

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.