View Full Version : Can anyone help me with a simple javascript code ?
pssheba
07-01-2009, 10:41 AM
Hi everyone
I inserted text box to my page. Onclick that text box activates a function.
If the text box is empty ("null" value i reckon) the function should pop an alert
notification that says: "value is null". This is the apparently wrong code
i wrote:
<script type="text/javascript">
<!--
function copyText(paramParam)
{
if(document.getElementById(paramParam).value==null)
{
alert("value is null")
}
}
-->
</script>
</head>
<body>
text1: <input type="text" id="myText" onclick="copyText('myText')">
</body>
Whatever i insert into the textbox i get no alert. In particular when
i insert nothing.
Can anyone show me where i got lost ?
Thanks.
coothead
07-01-2009, 11:06 AM
Hi there pssheba,
<input type="text"> has a default value so it will never equal null.
Use if(.....value==''){ alert() } instead.
I find your use of onclick rather confusing. :confused:
You have to click - focus - the text box to type in it. :agree:
This means that the alert will be triggered before your little pinkies have an opportunity to type. :eek:
What, actually, is it that you want to achieve?
pssheba
07-01-2009, 12:41 PM
Thank coothead,
I tried this and still, nothings happens. I 'd be grateful if you checked the following code:
<script type="text/javascript">
<!--
function copyText(paramParam)
{
if(document.getElementById(paramParam).value=='')
{
alert("value is null");
}
}
-->
</script>
</head>
<body>
text1: <input type="text" id="myText" onchange="copyText('myText')">
</body>
pssheba
07-01-2009, 12:49 PM
Please forgive me coothead !!! Your advice worked perfectly well !
this code works as you suggested !
<script type="text/javascript">
<!--
function copyText(paramParam)
{
if(document.getElementById(paramParam).value=='')
{
alert("value is null")
}
else
{
alert("value is not null")
}
}
-->
</script>
</head>
<body>
text1: <input type="text" id="myText" onmousedown="copyText('myText')">
Thanksa lot !!!
coothead
07-01-2009, 12:53 PM
Hi there pssheba,
you are having problems with you event handlers, aren't you. ;)
The onchange event handler is normally used on the select element.
The onmousedown event handler will have the same problem as the onclick.
When you click the <input type="text"> this gives it focus.
When you click somewhere else this removes the focus and is known as onblur.
pssheba
07-01-2009, 01:47 PM
Thanks coothead,
This is what i'm trying to learn : events, as you see, implementation of events i get complicatios. Hopefully it'll be clearer now and i'll not bother people with matching the right events to the right functions.
vBulletin® v3.6.7, Copyright ©2000-2009, Jelsoft Enterprises Ltd.