...

View Full Version : Resolved Running If Else with a User Input



MancunianMacca
03-04-2012, 03:14 PM
Hey, so i've been learning basics and I can get the following code to work as a prompt and I thought hey, I wounder if I can get it to work of a UI system, so I edited the code and got this

Read Below Post

Can you please help me to find where I am going wrong.
Thanks
MancunianMacca

MancunianMacca
03-04-2012, 03:22 PM
Ok so I have been experimanting more and have got it to recognise the user input but now it only displays the else option



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<script type="text/javascript">
function testing1(){
var testing
if (testing1 == "yes")
{
alert("Yay!");
}
else
{
alert("Yes it does");
}
}
</script>
<input type="text" onchange="testing1()" />
</body>
</html>

Philip M
03-04-2012, 03:54 PM
You must pass the value entered by the user to the function. It does not recognise the user input, but simple alerts the default.
var testing // this variable is not used
if (testing1 == "yes") // testing1 is the name of the function, not a variable.


<script type="text/javascript">

function testing1(testvalue){ // pass the entered value to the function
if (testvalue.toLowerCase()== "yes") { // make case-insensitive
alert ("Yay!");
}
else{
alert ("Yes it does");
}
}
</script>
<input type="text" onchange="testing1(this.value)" />
</body>
</html></body>
</html>


Quizmaster: What is the Latin name of the province in Canada which means "New Scotland" in English?
Contestant: Johannesburg.

MancunianMacca
03-04-2012, 04:11 PM
Works perfectly but so I am actually learning not just copying ould you please ecplain what the (this.value) does

Philip M
03-04-2012, 04:22 PM
onchange="testing1(this.value)"

When the value of the textbox is changed and loses focus, run the function testing1() and pass to that function the value of this element (the current object). In other words, the value that the user entered. OK?

MancunianMacca
03-04-2012, 04:39 PM
onchange="testing1(this.value)"

When the value of the textbox is changed and loses focus, run the function testing1() and pass to that function the value of this element (the current object). In other words, the value that the user entered. OK?

I'm honestly not trying to be annoying but i interpreted what you just said as " when the user has typed their answer, they can click off the text box and the function will run." am I right??

Philip M
03-04-2012, 05:29 PM
I'm honestly not trying to be annoying but i interpreted what you just said as " when the user has typed their answer, they can click off the text box and the function will run." am I right??

Yes. But I do think you ought to buy a book. This forum is not intended for remedial teaching.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum