BP Forums
Why Isn't this javascript code working? - Printable Version

+- BP Forums (https://bpforums.info)
+-- Forum: Archived Forums (https://bpforums.info/forumdisplay.php?fid=55)
+--- Forum: Archived Forums (https://bpforums.info/forumdisplay.php?fid=56)
+---- Forum: HTML/CSS/PHP (Web Development) (https://bpforums.info/forumdisplay.php?fid=23)
+---- Thread: Why Isn't this javascript code working? (/showthread.php?tid=667)



Why Isn't this javascript code working? - Himansh - 10-05-2012

I created a html document in which i added a predefined text in the textbox like "enter name here"
I created a function to delete the text when user clicks the input.
But , it had one problem that whenever user types his data and when he clicks the input then all his data is deleted. To prevent this used this method but this is not working , I don't know why.
here's the code

Code:
<!DOCTYPE html>
<html>

<head>


<script type="text/javascript">
function emptyfield(field){
field.value = "";
}

if(document.frmlogin.box.value != "Enter Username Here"){
frmlogin.box.onclick = "";
}
</script>
</head>

<body>

<form name="frmlogin" >
Username: <input type="text" id="cool" value="Enter Username Here" name="box" onclick="emptyfield(document.frmlogin.bo… />
</form>
</body>
</html>
I could have done it very easily using just one function by i want to know what is wrong in this.
I don't want alternative methods of doing it, i want to know what is wrong in this?

-Himansh


Re: Why Isn't this javascript code working? - brandonio21 - 10-05-2012

Well, I am by no means a master of whatever language this is written in. However, most languages are similar.

First of all, this line:
Code:
emptyfield(document.frmlogin.bo…

Seems to be incomplete. Could you provide us with the rest of the line? Maybe that is your problem!


Re: Why Isn't this javascript code working? - Himansh - 10-06-2012

Sorry that i didn't write the full code
That line actually is
emptyfield(document.frmlogin.box)

By the way, thanks for showing interest in my question buy i have already understood my mistake i made in this.

If you also want to see it
Then here's the link
<!-- m --><a class="postlink" href="http://answers.yahoo.com/question/index?qid=20121005120521AAl8QSb">http://answers.yahoo.com/question/index ... 521AAl8QSb</a><!-- m -->

Read the one i chose as best answer!

-Himansh


Re: Why Isn't this javascript code working? - brandonio21 - 10-07-2012

Ah, well that makes sense! Well, I'm glad that you got it figured out. But, for future reference, I recommend that you ask these type of questions on a website called "Stack Overflow" (<!-- m --><a class="postlink" href="http://stackoverflow.com/">http://stackoverflow.com/</a><!-- m -->). It is essentially Yahoo! Answers, but for programming.


Re: Why Isn't this javascript code working? - Chamez25 - 01-30-2013

Oh see, im glad that you've solved your own problem, <!-- sSmile --><img src="{SMILIES_PATH}/icon_e_smile.gif" alt="Smile" title="Smile" /><!-- sSmile --> just keep on posting your problen and we are glad to help.