...

View Full Version : Should be simple script? What's wrong here?



fuzzy1
12-01-2006, 03:09 PM
I've got an error here, but I can't see it?
Ridiculous as it may seem, all I want -- for testing purposes only -- is for the "checked" settings
in checkboxes 'gag' and new 'gag' to be overridden on load. Please, what's wrong with this script?

<script language="Javascript">
function gags_off(){
var gag =document.getElementById('gag');
var no_gag =document.getElementById('no_gag');
var new_gag =document.getElementById('new_gag');
var no_new_gag =document.getElementById('no_new_gag');

if(no_gag.checked=true)
{
gag.checked=false;
}
if(no_new_gag.checked=true)
{new_gag.checked=false
}
}
window.onload=gags_off();
</script>
gag:<input type="checkbox" id="gag" name="gag" value="1" checked>
no_gag:<input type="checkbox" id="no_gag" id="no_gag" value="1" checked>
new_gag:<input type="checkbox" id="new_gag" name="new_gag" value="1" checked>
no_new_gag:<input type="checkbox" id="no_new_gag" name="no_new_gag" value="1" checked>

nikkiH
12-01-2006, 03:55 PM
A few things.
javascript equality is "==" not "=".
But you didn't need that anyway.

Try this.
Note the order of the form elements and the script. The script comes AFTER, since it is not in the head section.



<body>
<form>
gag:<input type="checkbox" id="gag" name="gag" value="1" checked>
no_gag:<input type="checkbox" id="no_gag" name="no_gag" value="1" checked>
new_gag:<input type="checkbox" id="new_gag" name="new_gag" value="1" checked>
no_new_gag:<input type="checkbox" id="no_new_gag" name="no_new_gag" value="1" checked>
</form>

<script type="text/javascript">
function gags_off(){
var gag =document.getElementById('gag');
var no_gag =document.getElementById('no_gag');
var new_gag =document.getElementById('new_gag');
var no_new_gag =document.getElementById('no_new_gag');

if(no_gag.checked)
{
gag.checked=false;
}
if(no_new_gag.checked)
{
new_gag.checked=false
}
}
window.onload=gags_off
</script>
</body>

nikkiH
12-01-2006, 03:58 PM
That posted, here's an even shorter way, just for giggles.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> new document </title>
<script type="text/javascript">
function gags_off() {
var gag =document.getElementById('gag');
var no_gag =document.getElementById('no_gag');
var new_gag =document.getElementById('new_gag');
var no_new_gag =document.getElementById('no_new_gag');

gag.checked=!no_gag.checked;
new_gag.checked=!no_new_gag.checked
}
window.onload=gags_off
</script>

</head>

<body>
<form>
gag:<input type="checkbox" id="gag" name="gag" value="1" checked>
no_gag:<input type="checkbox" id="no_gag" name="no_gag" value="1" checked>
new_gag:<input type="checkbox" id="new_gag" name="new_gag" value="1" checked>
no_new_gag:<input type="checkbox" id="no_new_gag" name="no_new_gag" value="1" checked>
</form>

</body>
</html>

Kor
12-01-2006, 04:03 PM
A lot of errors:


<input type="checkbox" id="no_gag" name="no_gag" value="1" checked>
---------
if(no_gag.checked==true)
---------
if(no_new_gag.checked==true)
---------
window.onload=gags_off;


1. You gave gave, by mistake, an id twice, instead of an id and a name

2.3. The bitwise comparision operator is ==, not = (which is the simple asignment operator)

In fact you don't need that. The checked attribute has, in javascript, the Boolean values, thus it would be enough to:


if(no_gag.checked)
---------
if(no_new_gag.checked)
---------



4. If called as an object (a variable) a function looses it's paratnesis.

Kor
12-01-2006, 04:06 PM
Note the order of the form elements and the script. The script comes AFTER, since it is not in the head section.

As long as the function is triggered onload, it does not matter where the code lays, in head, in body before the objects, in body below the objects...

Kor
12-01-2006, 04:16 PM
... by the way fuzzy1
<script language="Javascript">

is a deprecated syntax. Now it is to be used the type as a script's attribute

<script type="text/javascript">

fuzzy1
12-01-2006, 05:17 PM
WOW! That's a lot to digest. Thanks Nikki, and Kor!
I've been coding now 10-14 hours a day for about a year,(mostly php, but a fair amount of js too) and just when I start to think I'm
getting a clue, I get smacked down by a silliy little bit of code like this.:rolleyes: I have a basic understanding of "==" as equal to,
and "=' as assignment, but as Nikki's examples attest, it doesn't seem to matter much in this context. That said, I always struggle with it (especially in php).

This is handy to note
if(no_gag.checked)
---------
if(no_new_gag.checked)
---------...and this is particularly nifty
gag.checked=!no_gag.checked;
new_gag.checked=!no_new_gag.checked
For all of my noobieness, I am however inclined to agree with Kor, in that -- triggered onload-- it shouldn't matter where the script lies?

As for name and id, I have seen where document.getElementById will call an element with only a "name" and no "id" explicitly defined suggesting that the two are in some regard interchangeable, but have come accross neumerous instances where BOTH are required to be defined, and by the same values (this being one of those instances) in php. Can you provide clarification?

And finally, I would be interested in clarification/example illustrating Kor's
assertion that " If called as an object (a variable) a function looses it's paratnesis."

Kor
12-01-2006, 05:54 PM
For all of my noobieness, I am however inclined to agree with Kor, in that -- triggered onload-- it shouldn't matter where the script lies?

Be sure I am right. onload (in fact the complete syntax is window.onload) trigger the function only after all the document was loaded. Thus anywhere the function might be placed, all the document's elements are already loaded when the function is called


As for name and id, I have seen where document.getElementById will call an element with only a "name" and no "id" explicitly defined suggesting that the two are in some regard interchangeable, but have come accross neumerous instances where BOTH are required to be defined, and by the same values (this being one of those instances) in php. Can you provide clarification?

IE particulary provides this inconsistency. If no name found, IE will take the id instead, but this is not a standard behaviour. name and id are different things. At least for the reason that id must be unique, while name may be the same for different elements (radio buttons group, for instance)


And finally, I would be interested in clarification/example illustrating Kor's
assertion that " If called as an object (a variable) a function looses it's paratnesis."


My interpretation is like that.

A function (same as a global variable) can be handle as a window's property. But functions are special objects, as they can bear (or not) arguments. If they don't caryy arguments, they are the same as global variables


<script type="text/javascript">
function myAlert(){
alert('Hello!')
}
onload=window['myAlert']
</script>

where
onload=window['myAlert']
can be written also as
onload=myAlert

Now, if a function carry arguments, things goes a little bit intricate, as a property must be separate by it's own properties or arguments. In this case, the JSON (http://www.json.org) way looks like:


<script type="text/javascript">
function myAlert(myargument){
alert(myargument)
}
onload=window['myAlert']('Hello!')
</script>

while the classical code must use an anonymous in order to permit the separation object - argument:


<script type="text/javascript">
function myAlert(myargument){
alert(myargument)
}
onload=function(){myAlert('Hello!')}
</script>

fuzzy1
12-01-2006, 05:56 PM
For what it's worth,
Albeit nifty, in context Nikki's 2nd example doesn't agree with the underlying php, causing 'gag' and 'new_gag' to return checked, when 'no_gag' or 'no_new_gag' are unchecked.:eek: So the first, and admitedly more cumbersome option panned out to be the script of choice.
Also, it does not in fact matter where the script lies in relation to the fields.

Thanks Again.
TJ

fuzzy1
12-01-2006, 06:02 PM
And Again, Thank You Kor!
I must have been writing my last post while you were posting yours.
I'm going to have to chew on all of that for bit before I'm able to digest:p
I do greatly appreciate the thoughtful and timely response.
Who Knows it might even help out some other poor schlub as well.:thumbsup:

Kind Regards,
TJ

nikkiH
12-01-2006, 08:38 PM
Also, it does not in fact matter where the script lies in relation to the fields.

Well, it did in MSIE 6 when I tested it. The object reference was null until I moved it.
Must have been some other mistake in there somewhere. Sorry about that.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum