checkbox issue

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
asterp
Forum Newbie
Posts: 1
Joined: Sat Jan 12, 2008 11:32 am

checkbox issue

Post by asterp »

I want to do something in Php and I am kind of stuck so I would greatly appreciate your help. Basically consider that you have an music album of songs. The album name and the song name have one checkbox per entry (i.e for every album name there is a checkbox and for every song name within the album name there is a checkbox) . Now what have done is pulled out entries from database and created php code for displaying all the entries in album. What I want to do is when a user checks the album name box all the individual checkboxes of song name should be disabled and when the user unchecks the album name box all entries should be enabled.

Do you have a way of dealing with this ?
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: checkbox issue

Post by VladSun »

Yes - use javascript.
You need onclick event and disabled property.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: checkbox issue

Post by RobertGonzalez »

Moved to Client Side.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: checkbox issue

Post by Jonah Bron »

Code: Select all

 
<input type="checkbox" onchange="if (this.checked)document.getElementById('othercheckbox').disabled=true; else document.getElementById('othercheckbox').disabled=false;" />
 
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: checkbox issue

Post by pickle »

~PHPyoungster's code will work if you only have one checkbox.

Save yourself a TON of grief and use the jQuery library to do this. Set up your code like this:

Code: Select all

<!-- Checkbox for album 123 --><input type = "checkbox" name = "album123" id = "album123" class = "album_checkbox" />......<!-- Checkboxes for songs in album 123 --><input type = "checkbox" name = "album123_song1" class = "album123_song"/><input type = "checkbox" name = "album123_song2" class = "album123_song"/><input type = "checkbox" name = "album123_song3" class = "album123_song"/><input type = "checkbox" name = "album123_song4"  class = "album123_song"/>
The important attributes here are the classes, as that's what you can use in jQuery to reference the different checkboxes.

Code: Select all

 /* Define the "anonymous" function to run when anything with the class "album_checkbox" is clicked */$(".album_checkbox").click(function(){     /* Extract the ID number from the clicked checkbox.  $(this) refers to the checkbox that was clicked.  */    var id = $(this).attr('id').substring(5);     /* If the clicked checkbox is checked, remove the "disabled" attribute" from the song checkboxes in this album */    if($(this).attr('checked'))        $(".album"+id+"_song").removeAttr("disabled");     /* If the checkbox is not checked, disable the song checkboxes in this album */    else        $(".album"+id+"_song").attr("disabled","disabled");});
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply