Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
144
rated 0 times [  145] [ 1]  / answers: 1 / hits: 7814  / 11 Years ago, tue, december 3, 2013, 12:00:00

For my code, I am trying to call 2 parameters into a function. Once you have left the 2nd input box, and multiply the 2 numbers passed through and put it in the third text box. if either of the first 2 input boxes are empty, then color them light red. This is my code so far, what am I missing?



Javascript:



function multiply(one, two) {
if(one==){
this.style.color='red';
}
if(two==){
this.style.color='red';
}
else{
txt1=one * two;
return txt1;
}

}


HTML5:



First Value: <input type=text name=mFirst />
Second Value: <input type=text name=mLast onblur=multiply(mFirst.value, mLast.value) />
Multiplication of First and Second Value: <input type=text name=answer>

More From » html

 Answers
13
<input … onblur=multiply.call(this,this.form.elements.mFirst.value,this.form.elements.mLast.value) >


function multiply(one, two) {
if(one && two){
this.form.elements.answer.value = one * two;
} else {
this.style.color='red';
}
}


Empty strings are non-truthy values, thus one && two will only be true if neither value is an empty string.



Using call allows you to set the value of this used inside the function.



Demo: http://jsfiddle.net/b5Ltt/



You might want to look at the HTML5 <output> element.


[#49904] Monday, December 2, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
iliana

Total Points: 246
Total Questions: 109
Total Answers: 82

Location: Palestine
Member since Tue, Jul 20, 2021
3 Years ago
;