Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-08-2004, 01:56 PM   PM User | #1
lordofthesword8
New Coder

 
Join Date: Jun 2004
Location: Coventry, UK
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
lordofthesword8 is an unknown quantity at this point
Question Checkboxes, Radio Buttons and Forms

1. With a checkbox in a form, using Javascript can you find out if its checked or not, and with a radio button, can you find out wich of thoose is selected?

2. Can you make a Form FIled un-editable using HTML or Javascript?

LOTS8
lordofthesword8 is offline   Reply With Quote
Old 07-08-2004, 02:23 PM   PM User | #2
Vincent Puglia
Regular Coder

 
Join Date: Jul 2003
Location: where the World once stood
Posts: 256
Thanks: 0
Thanked 2 Times in 2 Posts
Vincent Puglia is an unknown quantity at this point
Hi,

1) <input type='checkbox' name='chk' onclick='alert(this.checked)'>

<input type='checkbox' name='chk' onclick='doit(this)'>

function doit(theObj)
{
msg = (theObj.checked) ? "it's checked" : "it's not checked";
alert(msg)
}

2)
<form name='theForm'>
<input type='radio' name='rad' value='yes'>
<input type='radio' name='rad' value='no'>
<input type='button' value='Get Rad Value' onclick="doit(this.form)">
</form>

function doit(formObj)
{
for (var i = 0; i < formObj.length; i++)
{
if (formObj.elements[i].type == 'radio')
{
var el = formObj.elements[i];
if (el.checked)
alert(el.value)
}
}
}

3)
<input .... onfocus=this.blur()>
<input.....readonly>
<input.....disabled=true>

Vinny
__________________
mod at WebXperts
Vincent Puglia is offline   Reply With Quote
Old 07-08-2004, 07:16 PM   PM User | #3
lordofthesword8
New Coder

 
Join Date: Jun 2004
Location: Coventry, UK
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
lordofthesword8 is an unknown quantity at this point
Thanks

Thanks for 3, as it is really important. Could you please explain 1 and 2 a bit, though as I can see the logic.
__________________
LOTS8
lordofthesword8 is offline   Reply With Quote
Old 07-08-2004, 07:42 PM   PM User | #4
Vincent Puglia
Regular Coder

 
Join Date: Jul 2003
Location: where the World once stood
Posts: 256
Thanks: 0
Thanked 2 Times in 2 Posts
Vincent Puglia is an unknown quantity at this point
'this' is a javascript reserved word. For the most part it acts like a pointer and refers to whatever element it is in. It can also be used to point to some other element within the object's tree

'alert(this.checked)' --> the 'this' means the checkbox element/object
="doit(this.form)"> --> the 'this' means the button element and thus the form in which the button is found.


msg = (theObj.checked) ? "it's checked" : "it's not checked";

variable = (true/false conditional statement) ? ''result if condition is true" : "result if condition is false";

The first doit() function is called with onclick event handlers -- only the element concerned is being sent as a parameter so it's checked property can be tested immediately.

the second doit() function is called later (through the button). Since the entire form is passed, it is necessary to loop through the form's elements and find the desired objects (in actual code more testing would probably be necessary) Once found, the object's checked property is tested. (btw: Radios, by default, are arrays and any group of elements that have the same name will also be considered arrays by javascript -- and virtually every other language I know )

Vinny
__________________
mod at WebXperts
Vincent Puglia is offline   Reply With Quote
Old 07-09-2004, 11:12 AM   PM User | #5
lordofthesword8
New Coder

 
Join Date: Jun 2004
Location: Coventry, UK
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
lordofthesword8 is an unknown quantity at this point
Thanks

Thanks a lot for this
__________________
LOTS8
lordofthesword8 is offline   Reply With Quote
Old 07-09-2004, 12:14 PM   PM User | #6
Vincent Puglia
Regular Coder

 
Join Date: Jul 2003
Location: where the World once stood
Posts: 256
Thanks: 0
Thanked 2 Times in 2 Posts
Vincent Puglia is an unknown quantity at this point
You're welcome. if interested, http://www.htmlgoodies.com has a good series of online tutorials on javascript. You might also want to see: http://members.aol.com/grassblad -- it's my old site (scripts with explanations) that AOL was supposed to take down months ago.

Vinny
__________________
mod at WebXperts
Vincent Puglia is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:37 PM.


Advertisement
Log in to turn off these ads.