Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Regular Coder
    Join Date
    Apr 2006
    Posts
    311
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Should be simple script? What's wrong here?

    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?
    Code:
    <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>

  • #2
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    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.

    Code:
    <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>

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/

  • #3
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    That posted, here's an even shorter way, just for giggles.

    Code:
    <!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>

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/

  • #4
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    A lot of errors:
    Code:
    <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:
    Code:
    if(no_gag.checked)
    ---------
    if(no_new_gag.checked)
    ---------

    4. If called as an object (a variable) a function looses it's paratnesis.
    Last edited by Kor; 12-01-2006 at 04:08 PM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #5
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Quote Originally Posted by nikkiH
    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...
    Last edited by Kor; 12-01-2006 at 04:08 PM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #6
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    ... 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">
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #7
    Regular Coder
    Join Date
    Apr 2006
    Posts
    311
    Thanks
    17
    Thanked 0 Times in 0 Posts
    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. 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
    Code:
    if(no_gag.checked)
    ---------
    if(no_new_gag.checked)
    ---------
    ...and this is particularly nifty
    Code:
    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."

  • #8
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Quote Originally Posted by fuzzy1 View Post
    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
    Code:
    <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 way looks like:
    Code:
    <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:
    Code:
    <script type="text/javascript">
    function myAlert(myargument){
    alert(myargument)
    }
    onload=function(){myAlert('Hello!')}
    </script>
    Last edited by Kor; 12-01-2006 at 05:56 PM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #9
    Regular Coder
    Join Date
    Apr 2006
    Posts
    311
    Thanks
    17
    Thanked 0 Times in 0 Posts
    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. 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

  • #10
    Regular Coder
    Join Date
    Apr 2006
    Posts
    311
    Thanks
    17
    Thanked 0 Times in 0 Posts
    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
    I do greatly appreciate the thoughtful and timely response.
    Who Knows it might even help out some other poor schlub as well.

    Kind Regards,
    TJ

  • #11
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    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.

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •