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 13 of 13
  1. #1
    New Coder
    Join Date
    Feb 2012
    Posts
    51
    Thanks
    5
    Thanked 0 Times in 0 Posts

    using if() with a HTML form checkbox

    How come this doesn't work? I take it because 'unchecked' is not valid?

    Code:
    if (document.forms["form"]["targetname"].value == "" && document.forms["form"]["unknownname"] == "unchecked")

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,462
    Thanks
    0
    Thanked 633 Times in 623 Posts
    It is simplest if you use the id to reference form fields from JavaScript (you need them to attach the labels in the HTML so it isn't like you have to add anything extra).

    So if you had:

    Code:
    <input type="checkbox" id="chkbx1"><label for="chkbx1"> checkbox 1</label>
    then you can test if it is checked or not using

    Code:
    if (document.getElementById('chkbx1').checked) {...
    the value would be the same to JavaScript regardless of whether it is checked or not.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    New Coder
    Join Date
    Feb 2012
    Posts
    51
    Thanks
    5
    Thanked 0 Times in 0 Posts
    I was able to overcome it with:

    Code:
    onclick="document.forms['form']['targetname'].disabled = true"
    The problem I now face is that I can't get it to re-enable onclick again. I tried this but it didn't work:

    Code:
    <SCRIPT>
    	function unknownName() {
    		if (document.forms['form']['targetname'].disabled = false) {
    			document.forms['form']['targetname'].disabled = true;
    		}
    		if (document.forms['form']['targetname'].disabled = true) {
    			document.forms['form']['targetname'].disabled = false;
    		}
    	}
    </SCRIPT>
    Code:
    <INPUT name="unknownname" onclick="return unknownName()" type="checkbox">
    Any suggestions?
    Last edited by Noonga; 03-15-2012 at 03:02 AM.

  • #4
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    Quote Originally Posted by Noonga View Post
    How come this doesn't work? I take it because 'unchecked' is not valid?
    spot on

    The "checked" property is a boolean and so is set to true or false depending on whether the checkbox is checked or not. There is no attribute "unchecked".

    felgall has shown you the correct way to do it. Why are you ignoring it?
    Last edited by webdev1958; 03-15-2012 at 03:25 AM.

  • #5
    New Coder
    Join Date
    Feb 2012
    Posts
    51
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Why are you talking about something that has already been solved and irrelevant with the current issue? You still mad bro?
    Last edited by Noonga; 03-15-2012 at 03:38 AM.

  • #6
    New Coder
    Join Date
    Feb 2012
    Posts
    51
    Thanks
    5
    Thanked 0 Times in 0 Posts
    There was a problem where the if string equals were not double equals. I have fixed this, but the problem still exists:

    Code:
    function unknownName() {
    	if (document.forms["form"]["targetname"].disabled == false) {
    		document.forms["form"]["targetname"].disabled = true;
    	}
    	if (document.forms["form"]["targetname"].disabled == true) {
    		document.forms["form"]["targetname"].disabled = false;
    	}
    }
    Now my error console reports "unknownName is not defined". How is that so?!
    Last edited by Noonga; 03-15-2012 at 04:01 AM.

  • #7
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    Quote Originally Posted by Noonga View Post
    Now my error console reports "unknownName is not defined". How is that so?!
    Very easily .

    The error message cannot be any clearer. It is telling you that you are trying to call a function that hasn't been loaded yet.
    Last edited by webdev1958; 03-15-2012 at 04:20 AM.

  • #8
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    Quote Originally Posted by Noonga View Post
    You still mad bro?
    Not sure which of those who have replied you are referring to.

    Mad about what?

    But could you be referring to your post here where you state what you think of those trying to help you?

    It seems to me from the below that you have the bee under your bonnet and not anyone else

    I'm not the one who can't get along with people. Its most people on here.

    This is to be expected though. Computer programmers generally spend most of their life looking at a computer screen, away from interacting with the rest of the human race, so its not wonder computer programmers can't get along with the rest of society, such as myself.

    A trait of computer nerds is that, instead of helping others that ask for help, they turn it into some lame 'game' of clues and hints, when at the end of the day all someone wants is a straight answer.


    As for the band wagoning on here. Most computer nerds also lack confidence and self-esteem, and have generally been classed as a loser in society, so will hop onto the bandwagon momentum to make them self feel 'secure'.


    Computer nerds are pretty tragic.

    I know their are a small percent who have good social skills, but for the most part, a computer nerds logic goes something like this:

    Tourist: Hi, do you know where I can find the mall?


    Computer nerd: (Computer nerd knows answer already) Sure! If you walk down 500 metres, you will see a billboard. On the billboard you will find maps of the city, read from page 10 to page 60, and there you go. Hint hint, the pamphlet is green coloured.


    Where as a normal person who isn't a social drop out would just fricken tell the tourist and that would be that.


    Lectures of my behavior from the perspective of social drop outs doesn't enlighten me, it just entertains me.
    Last edited by webdev1958; 03-15-2012 at 04:43 AM.

  • #9
    New Coder
    Join Date
    Feb 2012
    Posts
    51
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Wow.. 20 minutes between each of those posts. You must have spent allot of time digging there.
    Yep, you still defiantly furious.. mid life crisis.
    Last edited by Noonga; 03-15-2012 at 01:55 PM.

  • #10
    New Coder
    Join Date
    Feb 2012
    Posts
    51
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Code:
    function unknownName() {
    	if (document.forms["form"]["targetname"].disabled == false) {
    		document.forms["form"]["targetname"].disabled = true;
    	} else {
    		document.forms["form"]["targetname"].disabled = false;
    	}
    }
    Solved.

  • #11
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    Quote Originally Posted by Noonga View Post
    ......You must have spent allot of time digging there.......
    Who are you talking to: felgall or me?

    If you're talking to me I wouldn't have told you what your error was earlier if I was mad

    In any case, digging for what? If you mean your post highlighing what you think of everyone who tries to help you then are you aware of how to use the forum's search function?

    If so, you should know that it takes about 10-15 seconds to enter a username and a keyword to find a post. I wouldn't call that "digging" at all let alone "a lot of digging"

    In this thread you posted:

    Quote Originally Posted by Noonga View Post
    .....Sorry for my poor sentance construction. My stress levels are at the ceiling due to imminent homelessness.
    I have no idea what your issues are, but taking out your frustrations in the forums for the situation you are in does nothing more than reflect badly on you from my point of view . I hope things work out ok for you eventually.
    Last edited by webdev1958; 03-15-2012 at 02:23 PM.

  • #12
    New Coder
    Join Date
    Feb 2012
    Posts
    51
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Some ultra editing done there. Cool assignment bro.

  • #13
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    This thread has become out of hand.
    Closing.


  •  

    Posting Permissions

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