Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
What is the mistake in this code... upon clicking the button nothing happens..


XML
<html>
<head>
<script>
JavaScript
function change()
{
    var t= document.getElementById("text1");
    if (t.value!=" ")
        windows.alert ("You entered: "+t.value);
    else
        windows.alert("would you enter some text);
}
XML
</script>
</head>
<body>
<input type= "text" id="text1">
<input type ="button" name = "b1" value = "click me" onclick ="change();">
</body>
</html>
Posted
Updated 27-Aug-15 6:18am
v2
Comments
Sergey Alexandrovich Kryukov 27-Aug-15 12:08pm    
It depends on what you want to achieve, but the code does not seem any reasonable.
—SA
[no name] 27-Aug-15 12:11pm    
sir i want to make a simple code in which someone can write a value and when click on "click me" it gives an alertbox saying "you entered + value" and if someone dont enter someone doesnt enter something then it says " would you enter some text"
Sergey Alexandrovich Kryukov 27-Aug-15 12:30pm    
I already gave you the answer, more comprehensive than the one you accepted as a solution (will you accept it formally, too?), where I also explain how to detect such problems and what's the problem with the functionality and the goal.
—SA

C#
function change() {
        var t = document.getElementById("text1");
        if (t.value != "")
            alert("You entered: " + t.value);
        else
            alert("would you enter some text");
    }


what you had wasn't even valid javacript. You should get a book on js and at least learn the basics as it's not a good way to learn to constantly ask people to fix your basic errors.
 
Share this answer
 
Comments
[no name] 27-Aug-15 12:24pm    
Thank you my friend i am new in all this so i am trying to learn its just wasnt getting to me for like 5 hours thank you no i get it
Sergey Alexandrovich Kryukov 27-Aug-15 12:31pm    
Just learning the language won't be enough. How to detect such bugs? Just human eye is not enough.
So, there is more to it; please see Solution 2.
—SA
The bug is lexical: in "would you enter some text", right quotation mark is missing.
Such bugs cannot be caught directly even by handling exceptions. The way to catch them as exception is explained in this chapter of my article: JavaScript Calculator, 5. Handling Lexical Errors.

Also, your code makes little to no practical sense: you consider input as "empty" only if " " is entered. How about entering "real empty" string, zero-length? how about two or more blank characters?

To improve this, you could use the string function .trim():
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim[^].

—SA
 
Share this answer
 
v5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900