set value in input from 3 different variables? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

set value in input from 3 different variables?

Any way to set value in input from different variables? Somethin' like this,but this one doesn't work :D $('#result').val(a operation b); Prefer JS or JQ solutions. Code: https://code.sololearn.com/W3X8ygh7Dt5K/#js

25th Apr 2017, 6:52 PM
Rose Sevenyears
Rose  Sevenyears - avatar
6 Answers
25th Apr 2017, 7:08 PM
Ashwani Kumar
Ashwani Kumar - avatar
+ 5
var a = 5; var b = 10; document.getElementById('result').setAttribute('value',a*b); This works. You can have operation also as a variable.
25th Apr 2017, 6:23 PM
Ashwani Kumar
Ashwani Kumar - avatar
+ 5
@Rose I think you need to use switch on the operation variable with different cases to work with different operations. I doubt if providing directly an operation as a string variable will work.
25th Apr 2017, 6:48 PM
Ashwani Kumar
Ashwani Kumar - avatar
+ 1
$('#result').val(a*b); document.getElementById('result').setAttribute('value',a*b); Both of them works same way...but when I try to use "operation" variable with + or - or / or * from selected option it doesn't work.
25th Apr 2017, 6:42 PM
Rose Sevenyears
Rose  Sevenyears - avatar
+ 1
You can check full code here ,also moved to question header. https://code.sololearn.com/W3X8ygh7Dt5K/#js Yeah...I thought about different operation with different conditions... If selected option + {$('#result').val(a+b);} e.t.c but...I still hope here's other way with just changing (arguments) :D Operation variable is string type,checked it too.
25th Apr 2017, 6:57 PM
Rose Sevenyears
Rose  Sevenyears - avatar
+ 1
Yep...that's the way too,thank you. Not that cool,but seems like only possible :)
25th Apr 2017, 7:19 PM
Rose Sevenyears
Rose  Sevenyears - avatar