Forum Moderators: open

Message Too Old, No Replies

getElementById with error

         

romzinho2k7

11:56 am on Sep 7, 2006 (gmt 0)

10+ Year Member



Good morning

It was trying to update a JavaScript's Function, but of the mistake.

The function:
------------------------------------------
function Mark(which){
if (eval("document.form." + which + ".checked")){
eval("tr" + which + "getElementById(" + which + ").style.background = '#D2E9FF'")
} else {
eval("tr" + which + "getElementById(" + which + ").style.background = ''");
if((which!= 'principal')&&(document.form.principal.checked)) {
document.form.principal.checked = false;
Mark('principal')
}
}
}
------------------------------------------

It is giving mistake in the two lines that contains getElementById.

I want to use him because W3C only been worth if it has this function.

sorry my english.

thanks.

bird

12:08 pm on Sep 7, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It seems there is a dot missing before the function name.

"getElementById("

should be

".getElementById("

<added>Most likely they should also be a dot after the "tr"</added>

[edited by: bird at 12:15 pm (utc) on Sep. 7, 2006]

daveVk

12:11 pm on Sep 7, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



replace

eval("tr" + which + "getElementById(" + which + ").style.background = '#D2E9FF'")

with

window.document.getElementById( which ).style.background = '#D2E9FF';

and similar for else case

bird

12:16 pm on Sep 7, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well yes, that makes even more sense... ;)

romzinho2k7

10:13 pm on Sep 7, 2006 (gmt 0)

10+ Year Member



Hi, thanks

Do not worked. He selected only the table that has checkbox and is not working on Firefox. I am going to pass the whole code for you see.

--------------------------------------------
<!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=iso-8859-1" />
<title>CheckBox - Test</title>

<script type="text/javascript">
function SelectAll() {
for (var i=0;i<document.form.elements.length;i++) {
var x = document.form.elements[i];
if (x.id == 'principal') {
x = document.form.principal.checked;
}
if(x.type == 'checkbox') {
x.checked = document.form.principal.checked;
Mark(x.id)
}
}
}

function Mark(which){
if (eval("document.form." + which + ".checked")){
window.document.getElementById( which ).style.background = '#D2E9FF';
} else {
window.document.getElementById( which ).style.background = '';
if((which!= 'principal')&&(document.form.principal.checked)) {
document.form.principal.checked = false;
Mark('principal')
}
}
}
</script>
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><form name="form" method="post" action="">
<table width="100%" cellspacing="2">
<tr id="trprincipal">
<td width="2%"><input type="checkbox" id="principal" name="principal" onClick="SelectAll()" /></td>
<td width="98%">&nbsp;</td>
</tr>
<tr id="trq05">
<td><input onClick="Mark('trq05')" type="checkbox" id="q05" name="aaaa" value="001" /></td>
<td>&nbsp;</td>
</tr>
<tr id="trq02">
<td><input onClick="Mark('trq02')" type="checkbox" id="q02" name="bbb" value="001" /></td>
<td>&nbsp;</td>
</tr>
<tr id="trq03">
<td><input onClick="Mark('trq03')" type="checkbox" id="q03" name="ccc" value="001" /></td>
<td>&nbsp;</td>
</tr>
<tr id="trq04">
<td><input onClick="Mark('trq04')" type="checkbox" id="q04" name="ddd" value="001" /></td>
</tr>
</table>
</form>
</td>
</tr>
</table>
</body>
</html>
----------------------------------------------------

Copy and paste that code and see. In the IE works partially. He does not select for tag <tr></tr> all. And in the firefox does not work.

Thanks.

daveVk

12:45 am on Sep 8, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



if (x.id == 'principal') {
x = document.form.principal.checked;
}

perhaps you intended?

if (x.id == 'principal') {
x.checked = document.form.principal.checked;
}