Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
| Search | Today's Posts | Mark Forums Read
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old October 13th, 2005, 07:48 PM
Authorized User
 
Join Date: Aug 2005
Location: , , .
Posts: 64
Thanks: 0
Thanked 0 Times in 0 Posts
Default dynamically enable/disable radio btns

hi again
below, i m pasting some html code ... in this html, i've created 15 radio btns, "r1" to "r15" ... and in the end ... there is a link to a function enabledisable()
when the function is called ... it generates a random number from 1 to 15 (i've did this too) ... now ... what i've 2 do iz that ... whatever the random number comes ... all the radio btns from "r1" till that random number should become ENABLED & CHECKED !!! got my point ???
i mean that if the number comes 7 ... then all the radio btns from "r1" to "r7" should become ENABLED + CHECKED !!!
every time when V click the link ... it should do the same
i hope u've got my point N u'll help me

<html>
<head>
<title>Enable / Disable</title>
</head>
<script language="JavaScript">
function enabledisable()
{
    num=Math.ceil(Math.random()*15);
    // WHAT TO DO AFTER THIS ???
}
</script>
<body>
<form name="radioForm">
<input type="radio" name="r1" value="r1" DISABLED>Radio 1
<br>
<input type="radio" name="r2" value="r2" DISABLED>Radio 2
<br>
<input type="radio" name="r3" value="r3" DISABLED>Radio 3
<br>
<input type="radio" name="r4" value="r4" DISABLED>Radio 4
<br>
<input type="radio" name="r5" value="r5" DISABLED>Radio 5
<br>
<input type="radio" name="r6" value="r6" DISABLED>Radio 6
<br>
<input type="radio" name="r7" value="r7" DISABLED>Radio 7
<br>
<input type="radio" name="r8" value="r8" DISABLED>Radio 8
<br>
<input type="radio" name="r9" value="r9" DISABLED>Radio 9
<br>
<input type="radio" name="r10" value="r10" DISABLED>Radio 10
<br>
<input type="radio" name="r11" value="r11" DISABLED>Radio 11
<br>
<input type="radio" name="r12" value="r12" DISABLED>Radio 12
<br>
<input type="radio" name="r13" value="r13" DISABLED>Radio 13
<br>
<input type="radio" name="r14" value="r14" DISABLED>Radio 14
<br>
<input type="radio" name="r15" value="r15" DISABLED>Radio 15
<br>
<a href="javascript:enabledisable()">Enable / Disable</a>
</form>
</body>
</html>

w8ing 4 a good reply

  #2 (permalink)  
Old October 14th, 2005, 10:51 AM
Friend of Wrox
 
Join Date: May 2004
Location: , , .
Posts: 212
Thanks: 0
Thanked 0 Times in 0 Posts
Default

This should do it:


<form name="radioForm">
<input type="radio" id="r1" value=1 DISABLED>Radio 1
<br>
<input type="radio" id="r2" value=2 DISABLED>Radio 2
<br>
<input type="radio" id="r3" value=3 DISABLED>Radio 3
<br>
<input type="radio" id="r4" value=4 DISABLED>Radio 4
<br>
<input type="radio" id="r5" value=5 DISABLED>Radio 5
<br>
<input type="radio" id="r6" value=6 DISABLED>Radio 6
<br>
<input type="radio" id="r7" value=7 DISABLED>Radio 7
<br>
<input type="radio" id="r8" value=8 DISABLED>Radio 8
<br>
<input type="radio" id="r9" value=9 DISABLED>Radio 9
<br>
<input type="radio" id="r10" value=10 DISABLED>Radio 10
<br>
<input type="radio" id="r11" value=11 DISABLED>Radio 11
<br>
<input type="radio" id="r12" value=12 DISABLED>Radio 12
<br>
<input type="radio" id="r13" value=13 DISABLED>Radio 13
<br>
<input type="radio" id="r14" value=14 DISABLED>Radio 14
<br>
<input type="radio" id="r15" value=15 DISABLED>Radio 15
<br>
<a href="javascript:enabledisable()">Enable / Disable</a>
</form>


<script language ="javascript">
function enabledisable(){


var numa = Math.ceil(Math.random()*15);


for (var i = 1; i<numa+1; i++){
document.getElementById("r" + i).disabled = false;
document.getElementById("r" + i).checked = true;
}

for(var j =numa+1; j<=15; j++){
document.getElementById("r" + j).disabled = true;
document.getElementById("r" + j).checked = false;
}


}
</script>

HTH!
Joe
  #3 (permalink)  
Old October 17th, 2005, 08:04 AM
Friend of Wrox
Points: 8,019, Level: 38
Points: 8,019, Level: 38 Points: 8,019, Level: 38 Points: 8,019, Level: 38
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2005
Location: Mauchline, East Ayrshire, Scotland
Posts: 1,525
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to crmpicco Send a message via AIM to crmpicco Send a message via MSN to crmpicco Send a message via Yahoo to crmpicco
Default

as it is a radio button use getElementById("idinhere") to refer to each individual element

www.crmpicco.co.uk
  #4 (permalink)  
Old October 17th, 2005, 10:16 AM
Friend of Wrox
 
Join Date: May 2004
Location: , , .
Posts: 212
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Picco,

In my script I am doing that, but simply concatenating the id from the predefined string "r" and the number of the radio button which is (i) or (j) in my for loops. Like so:


document.getElementById("r" + i)

<input type="radio" id="r11" value=11 DISABLED>Radio 11

In the <input tag> its ID is "r11", so I make the id from specifying the string "r" and the value from the for loop (1 to 15, in this case 11).

Try the script, it does work. ;)

Cheers
Joe




Similar Threads
Thread Thread Starter Forum Replies Last Post
Using JS to enable/disable?? crabjoe Javascript 4 November 10th, 2008 06:48 PM
enable/disable textfields based on radio selection carrie.mims Javascript How-To 5 December 8th, 2006 05:41 AM
Enable Radio when check on Checkbox... rupen Javascript How-To 2 June 28th, 2006 04:44 AM
Disable and Enable at runtime Hammam ASP.NET 1.0 and 1.1 Professional 0 May 17th, 2006 12:40 AM
Disable/Enable elements ldoodle Javascript How-To 3 June 7th, 2005 05:48 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.