...

View Full Version : using if() with a HTML form checkbox



Noonga
03-15-2012, 02:33 AM
How come this doesn't work? I take it because 'unchecked' is not valid?


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

felgall
03-15-2012, 02:45 AM
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:


<input type="checkbox" id="chkbx1"><label for="chkbx1"> checkbox 1</label>

then you can test if it is checked or not using


if (document.getElementById('chkbx1').checked) {...

the value would be the same to JavaScript regardless of whether it is checked or not.

Noonga
03-15-2012, 02:52 AM
I was able to overcome it with:


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:



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



<INPUT name="unknownname" onclick="return unknownName()" type="checkbox">


Any suggestions?

webdev1958
03-15-2012, 03:21 AM
How come this doesn't work? I take it because 'unchecked' is not valid?

spot on :thumbsup:

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?

Noonga
03-15-2012, 03:29 AM
Why are you talking about something that has already been solved and irrelevant with the current issue? You still mad bro?

Noonga
03-15-2012, 03:55 AM
There was a problem where the if string equals were not double equals. I have fixed this, but the problem still exists:



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?!

webdev1958
03-15-2012, 04:17 AM
Now my error console reports "unknownName is not defined". How is that so?!

Very easily :thumbsup:.

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. :)

webdev1958
03-15-2012, 04:37 AM
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 (http://www.codingforums.com/showpost.php?p=1200474&postcount=17) 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.

Noonga
03-15-2012, 01:50 PM
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.

Noonga
03-15-2012, 01:53 PM
function unknownName() {
if (document.forms["form"]["targetname"].disabled == false) {
document.forms["form"]["targetname"].disabled = true;
} else {
document.forms["form"]["targetname"].disabled = false;
}
}


Solved.

webdev1958
03-15-2012, 02:08 PM
......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" :thumbsup:

In this thread (http://www.codingforums.com/showpost.php?p=1204179&postcount=1) you posted:



.....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.

Noonga
03-15-2012, 11:57 PM
Some ultra editing done there. Cool assignment bro.

Fou-Lu
03-16-2012, 12:41 AM
This thread has become out of hand.
Closing.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum