/    Sign up×
Community /Pin to ProfileBookmark

CheckBoxList javascript problem

Hi

I’m creating a search page that enables the user to make multi choices.

So I have to do it with CheckBoxList

like that

<asp:CheckBoxList ID=”CheckBoxList1″ runat=”server”>
<asp:ListItem Value=”0″>None</asp:ListItem>
<asp:ListItem Value=”1″>English</asp:ListItem>
<asp:ListItem Value=”2″>German</asp:ListItem>
<asp:ListItem Value=”3″>French</asp:ListItem>
</asp:CheckBoxList>

what I want is when I click on “None” option it clears all other options

and if I click on any other option like (“English” or “German” or “French”) it clears the “None” option

I have 5 CheckBoxLists on my page

and I Work under MasterPage

I had this code with jquery but I don’t know what the problem here

<asp:Content ID=”Content1″ ContentPlaceHolderID=”ContentPlaceHolder1″ Runat=”Server”>

<script src=”http://code.jquery.com/jquery-latest.js”></script>
<script type=”text/javascript”>
//function doHourglass() { document.body.style.cursor = ‘wait’; }
$(document).ready(function(){
$(“INPUT[name=’CheckBoxList1′]”).click(function(){
if($(this).is(‘:checked’)){
if($(this).val()==”0″){
$(“INPUT[name=’CheckBoxList1′]”).each(function(){
if($(this).val()!=”0″){ $(this).attr(‘checked’,false); }
});
}
else
{
$(“INPUT[name=’CheckBoxList1′]:first”).attr(‘checked’,false);
}
}
});
});
</script>

<asp:CheckBoxList ID=”CheckBoxList1″ runat=”server” >
<asp:ListItem Value=”0″>None</asp:ListItem>
<asp:ListItem Value=”1″>English</asp:ListItem>
<asp:ListItem Value=”2″>German</asp:ListItem>
<asp:ListItem Value=”3″>French</asp:ListItem>
</asp:CheckBoxList>

</asp:Content>

please could you help me to find the problem

Your help will be appreciated

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@Gozzy82Mar 09.2010 — i used normal checkboxes to test but afaik asp.net outputs those checkboxes to this.

<i>
</i>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
&lt;title&gt;Test&lt;/title&gt;

&lt;script src="http://code.jquery.com/jquery-latest.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript"&gt;
$(function(){

$("input[name='language']").each(function(){ $(this).click(checkChecks); });

});

function checkChecks(){
if($(this).is(':checked')){
if($(this).val()=="0"){
$("INPUT[name='language']").each(function(){
if($(this).val()!="0"){
$(this).attr('checked',false);
}
});
}
else
{
$("input[name='language']:first").attr('checked',false);
}
}
}

&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;label&gt;
&lt;input type="checkbox" checked="checked" name="language" value="0" /&gt;None
&lt;/label&gt;
&lt;label&gt;
&lt;input type="checkbox" name="language" value="1" /&gt;English
&lt;/label&gt;
&lt;label&gt;
&lt;input type="checkbox" name="language" value="2" /&gt;German
&lt;/label&gt;
&lt;label&gt;
&lt;input type="checkbox" name="language" value="3" /&gt;French
&lt;/label&gt;

&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@javaneesauthorMar 09.2010 — Hi Gozzy82

I know that the normal checkboxs are working but I want the asp.net CheckBoxList Control

to work as the same as the normal checkboxs.

when i call the checkboxlist name here $("input[name='????????']")

it doesn't work .

the question is how can call it here.
Copy linkTweet thisAlerts:
@Gozzy82Mar 10.2010 — if you look at the html code your asp generates you can see the name of the input type="checkbox"s iirc that changes to something like ctl00_ctl00_ContentPlaceHolder1_ContentPlaceHolder1_inputName

you can get that name in javascript by doing something like

var checkboxname = '<%= checkboxName.ClientID %>';

or you can put the checkboxlist inside a div with an id and get it from there

<div id="checkboxlistycontainer">

<asp:CheckBoxList ID="CheckBoxList1" runat="server">

<asp:ListItem Value="0">None</asp:ListItem>

<asp:ListItem Value="1">English</asp:ListItem>

<asp:ListItem Value="2">German</asp:ListItem>

<asp:ListItem Value="3">French</asp:ListItem>

</asp:CheckBoxList>

</div>

and in jquery

var checkbox = $('#checkboxlistycontainer input');
Copy linkTweet thisAlerts:
@javaneesauthorMar 10.2010 — Hi Gozzy82

unfortunately it doesn't work also
Copy linkTweet thisAlerts:
@Gozzy82Mar 10.2010 — can you show the html your asp.net generates?
Copy linkTweet thisAlerts:
@javaneesauthorMar 11.2010 — ok here the page source





<script src="http://code.jquery.com/jquery-latest.js"></script>

<script type="text/javascript">

//function doHourglass() { document.body.style.cursor = 'wait'; }

$(document).ready(function(){

$("INPUT[name='CheckBoxList1']").click(function(){

if($(this).is(':checked')){

if($(this).val()=="0"){

$("INPUT[name='CheckBoxList1']").each(function(){

if($(this).val()!="0"){ $(this).attr('checked',false); }

});

}

else

{

$("INPUT[name='CheckBoxList1']:first").attr('checked',false);

}

}

});

});

</script>


<table id="ctl00_ContentPlaceHolder1_CheckBoxList1" border="0">

<tr>

<td><input id="ctl00_ContentPlaceHolder1_CheckBoxList1_0" type="checkbox" name="ctl00$ContentPlaceHolder1$CheckBoxList1$0" /><label for="ctl00_ContentPlaceHolder1_CheckBoxList1_0">None</label></td>

</tr><tr>

<td><input id="ctl00_ContentPlaceHolder1_CheckBoxList1_1" type="checkbox" name="ctl00$ContentPlaceHolder1$CheckBoxList1$1" /><label for="ctl00_ContentPlaceHolder1_CheckBoxList1_1">English</label></td>

</tr><tr>
<td><input id="ctl00_ContentPlaceHolder1_CheckBoxList1_2" type="checkbox" name="ctl00$ContentPlaceHolder1$CheckBoxList1$2" /><label for="ctl00_ContentPlaceHolder1_CheckBoxList1_2">German</label></td>
</tr><tr>
<td><input id="ctl00_ContentPlaceHolder1_CheckBoxList1_3" type="checkbox" name="ctl00$ContentPlaceHolder1$CheckBoxList1$3" /><label for="ctl00_ContentPlaceHolder1_CheckBoxList1_3">French</label></td>
</tr>

</table>
Copy linkTweet thisAlerts:
@Gozzy82Mar 11.2010 — hmz you have to alter the code quite a bit because your checkboxes dont even have a value..

this one works, check out the code to see how...

http://www.webdevout.net/test?032

<i>
</i>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
&lt;title&gt;Test&lt;/title&gt;

&lt;script src="http://code.jquery.com/jquery-latest.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript"&gt;
$(function(){
$("input[name^='ctl00$ContentPlaceHolder1$CheckBoxList1']").each(function(){ $(this).click(checkChecks); });
});

function checkChecks(){
if($(this).is(':checked')){

if($(this).next('label').text()=="None"){
$("INPUT[name^='ctl00$ContentPlaceHolder1$CheckBoxList1']").each(function(){
if($(this).next('label').text()!="None"){
$(this).attr('checked',false);
}
});
}
else
{
$("input[name^='ctl00$ContentPlaceHolder1$CheckBoxList1']:first").attr('checked',false);
}
}
}


&lt;/script&gt;

&lt;/head&gt;
&lt;body&gt;


&lt;table id="ctl00_ContentPlaceHolder1_CheckBoxList1" border="0"&gt;
&lt;tr&gt;
&lt;td&gt;&lt;input id="ctl00_ContentPlaceHolder1_CheckBoxList1_0" type="checkbox" name="ctl00$ContentPlaceHolder1$CheckBoxList1$0" /&gt;&lt;label for="ctl00_ContentPlaceHolder1_CheckBoxList1_0"&gt;None&lt;/label&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
&lt;td&gt;&lt;input id="ctl00_ContentPlaceHolder1_CheckBoxList1_1" type="checkbox" name="ctl00$ContentPlaceHolder1$CheckBoxList1$1" /&gt;&lt;label for="ctl00_ContentPlaceHolder1_CheckBoxList1_1"&gt;English&lt;/label&gt;&lt;/td&gt;

&lt;/tr&gt;&lt;tr&gt;
&lt;td&gt;&lt;input id="ctl00_ContentPlaceHolder1_CheckBoxList1_2" type="checkbox" name="ctl00$ContentPlaceHolder1$CheckBoxList1$2" /&gt;&lt;label for="ctl00_ContentPlaceHolder1_CheckBoxList1_2"&gt;German&lt;/label&gt;&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
&lt;td&gt;&lt;input id="ctl00_ContentPlaceHolder1_CheckBoxList1_3" type="checkbox" name="ctl00$ContentPlaceHolder1$CheckBoxList1$3" /&gt;&lt;label for="ctl00_ContentPlaceHolder1_CheckBoxList1_3"&gt;French&lt;/label&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@javaneesauthorMar 11.2010 — [COLOR="SeaGreen"][B]Hiiiiiiiiii Gozzy82



Thank you very very very much



it works perfectly



really I appreciate your help



thank ya man[/B]
[/COLOR]
×

Success!

Help @javanees spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...