views:

306

answers:

5

I am using

var txtname=document.getElementById('<%=txtname.ClientID%>').value;    
if (txtName == "") ....

but in FF this is not working. This condition never become true. Even if it has no value. In IE and Chrome its working fine.

Can anyone let me know how to solve this.....

A: 

use === instead of ==.

erenon
He's using javascript, === is not an operator in javascript.
tschaible
=== is an operator (means: is identical to). But this is not the issue here
Philippe Leybaert
@tschable: === is a valid javascript operator.
Joel Coehoorn
@activa and @Joel ... I was mistaken thank you for correcting.
tschaible
A: 

Looks like you are using some server side language of some sort mixed with javascript.

What does txtname.ClientID actually evaluate to and what is on your page.

It is very likely that you are not selecting an actual element with the document.getElementById call.

Chrome and IE may handle this one way (return an empty string when you attempt to access value)

while Firefox may handle it another way (return null, return an error when attempting to get value from the null object, and stop processing javascript).

To see how this works, try opening up the file below in firefox. The first two attempts (when a null check is in place) will correctly report the found or not found conditions.

With the second two attempts (when a null check is not in place), the first (element found) works fine, but the second fails because null is returned by document.getElementById and firefox does not know how to continue from this error.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd"&gt;
<html>
<head>
<title>CSS Reset</title>


</head>
<body>
<input id="value1" type="text" value="value1"/>

<script language="javascript">

alert('finding value1 with null check');
var element = document.getElementById('value1');
if ( element == null ){
    alert('not found');
}
else{
    var txtname = element.value;
    alert(txtname);
}

alert('finding value2 with null check');
var element = document.getElementById('value2');
if ( element == null ){
    alert('not found');
}
else{
    var txtname = element.value;
    alert(txtname);
}


alert('finding value1 without null check');
var txtname = document.getElementById('value1').value;
alert(txtname);

alert('finding value2 without null check');
txtname = document.getElementById('value2').value;
alert(txtname);

alert('finished');
</script>

</body>
</html>
tschaible
txtname.ClientID evaluates to the element's id once it's been rendered to the client. That part is just fine.
Tor Haugen
But what does it evaluate/render to? Depending on if the element is not present on the page or if the ID is already used by another element, errors can be introduced.
tschaible
It renders to an ugly string. .NET renames controls on the page so you do not have conflicts, and the way to get the generated id is through that mess.
epascarello
A: 

Make sure that you don't have 2 elements with the same id (or name!.. read below).

Wouter van Nifterick
A: 

Although this may not fix the problem, you can change the comparison to:

if (!txtName)
  ....

This will succeed if txtName is null, undefined or an empty string.

Philippe Leybaert
A: 

You should check the error console of ff. Press Strg+Shift+J and check if an error occurd. Also the FF Plug in Firebug could help you debugging javascript. It provides a debugmode like ide's do.

And <%=...%> allows to use a javacode expression on a JSP, are you sure that this is a correct id, that allows to use document.getElementById?

Donar