/    Sign up×
Community /Pin to ProfileBookmark

Javascript can´t work with a specific Id type

Hi to all,

i´m new to the forum and also to javascript

i´m having a problem that seems very simple, yet i can´t get it to work

the thing is i wan´t to put in my website a Mailchimp form so users can subscribe to my email list

i´ve used the form they provide, but in the form there is several checkboxes were the subscriber can select wich subjects he is interest to receive email from us

and the the bottom of this checkboxes there is an option “all of them”

so what i want is when the person clicks on the “all of them” option all others gets unchecked and disabled ( that´s because i want him to subscribe to the list “all” and not to all lists)

i have this code that is working fine

[CODE]
<div id=”mc_embed_signup”>
<form action=”http://bfkfskfgkf.us2.list-manage1.com/” method=”post” id=”mc-embedded-subscribe-form” name=”mc-embedded-subscribe-form” class=”validate” target=”_blank”>
<fieldset>

<br/>
<div class=”mc-field-group”>
<label class=”input-group-label”>interesse </label>
<div class=”input-group” >

<table id=”intere” width=”590″ cellspacing=”1″ >

<tr >
<td width=”176″><label>
<input type=”checkbox” value=”1″ name=”group[1][1]” id=”option0″>
<label for=”mce-group[1]-1-0″>Area 1</label></td>
<td width=”105″><input type=”checkbox” value=”2″ name=”group[1][2]” id=”option1″><label for=”mce-group[1]-1-1″>Area 2</label></td>
<td width=”141″><input type=”checkbox” value=”4″ name=”group[1][4]” id=”option2″><label for=”mce-group[1]-1-2″>Area 3</label></td>
<td width=”153″><input type=”checkbox” value=”8″ name=”group[1][8]” id=”option3″><label for=”mce-group[1]-1-3″>Area 4</label></td>
</tr>
<tr>
<td><input type=”checkbox” value=”65536″ name=”group[1][65536]” id=”option4″><label for=”mce-group[1]-1-4″>Area 5</label></td>
<td><input type=”checkbox” value=”16″ name=”group[1][16]” id=”option5″><label for=”mce-group[1]-1-5″>Area 6</label></td>
<td><input type=”checkbox” value=”32″ name=”group[1][32]” id=”option6″><label for=”mce-group[1]-1-6″>Area 7</label></td>
<td><input type=”checkbox” value=”64″ name=”group[1][64]” id=”option7″><label for=”mce-group[1]-1-7″>Area 8</label></td>
</tr>

</table>
<input type=”checkbox” value=”32768″ name=”group[1][32768]” id=”opcao8″ onclick=”jqCheck(this.id, ‘intere’);”><label for=”mce-group[1]-1-16″>Todas</label>
</div>
</div>

<br/>

<div id=”mce-responses”>
<div class=”response” id=”mce-error-response” style=”display:none”></div>
<div class=”response” id=”mce-success-response” style=”display:none”></div>
</div>
<div><input type=”submit” value=”Enviar” name=”subscribe” id=”mc-embedded-subscribe” class=”btn”></div>
</fieldset>
<a href=”#” id=”mc_embed_close” class=”mc_embed_close”></a>
</form>
</div>
<script type=”text/javascript” src=”http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js”></script>
<script type=”text/javascript”>

function jqCheck( id, pID )
{
$( “#” + pID + ” :checkbox”).attr(‘disabled’, $(‘#’ + id).is(‘:checked’)).attr(‘checked’, $(‘#’ + id).is(‘:disabled’));

}
</script>

[/CODE]

notice that all checkboxes have the id like “option1, option2, option3…”

but if i use the ids Mailchimp give to his form “mce-group[1]-1-1, mce-group[1]-1-1…” as bellow, the script no longer works properly. How can i get it to work?

[CODE]
<div id=”mc_embed_signup”>
<form action=”http://bfkfskfgkf.us2.list-manage1.com/” method=”post” id=”mc-embedded-subscribe-form” name=”mc-embedded-subscribe-form” class=”validate” target=”_blank”>
<fieldset>

<br/>
<div class=”mc-field-group”>
<label class=”input-group-label”>interesse </label>
<div class=”input-group” >

<table id=”intere” width=”590″ cellspacing=”1″ >

<tr >
<td width=”176″><label>
<input type=”checkbox” value=”1″ name=”group[1][1]” id=”mce-group[1]-1-0″>
<label for=”mce-group[1]-1-0″>Area 1</label></td>
<td width=”105″><input type=”checkbox” value=”2″ name=”group[1][2]” id=”mce-group[1]-1-1″><label for=”mce-group[1]-1-1″>Area 2</label></td>
<td width=”141″><input type=”checkbox” value=”4″ name=”group[1][4]” id=”mce-group[1]-1-2″><label for=”mce-group[1]-1-2″>Area 3</label></td>
<td width=”153″><input type=”checkbox” value=”8″ name=”group[1][8]” id=”mce-group[1]-1-3″><label for=”mce-group[1]-1-3″>Area 4</label></td>
</tr>
<tr>
<td><input type=”checkbox” value=”65536″ name=”group[1][65536]” id=”mce-group[1]-1-4″><label for=”mce-group[1]-1-4″>Area 5</label></td>
<td><input type=”checkbox” value=”16″ name=”group[1][16]” id=”mce-group[1]-1-5″><label for=”mce-group[1]-1-5″>Area 6</label></td>
<td><input type=”checkbox” value=”32″ name=”group[1][32]” id=”mce-group[1]-1-6″><label for=”mce-group[1]-1-6″>Area 7</label></td>
<td><input type=”checkbox” value=”64″ name=”group[1][64]” id=”mce-group[1]-1-7″><label for=”mce-group[1]-1-7″>Area 8</label></td>
</tr>

</table>
<input type=”checkbox” value=”32768″ name=”group[1][32768]” id=”mce-group[1]-1-8″ onclick=”jqCheck(this.id, ‘intere’);”><label for=”mce-group[1]-1-16″>Todas</label>
</div>
</div>

<br/>

<div id=”mce-responses”>
<div class=”response” id=”mce-error-response” style=”display:none”></div>
<div class=”response” id=”mce-success-response” style=”display:none”></div>
</div>
<div><input type=”submit” value=”Enviar” name=”subscribe” id=”mc-embedded-subscribe” class=”btn”></div>
</fieldset>
<a href=”#” id=”mc_embed_close” class=”mc_embed_close”></a>
</form>
</div>
<script type=”text/javascript” src=”http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js”></script>
<script type=”text/javascript”>

function jqCheck( id, pID )
{
$( “#” + pID + ” :checkbox”).attr(‘disabled’, $(‘#’ + id).is(‘:checked’)).attr(‘checked’, $(‘#’ + id).is(‘:disabled’));

}
</script>
[/CODE]

thnks everyone

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@coldfire421Apr 05.2011 — [code=html]
<html>
<head>
<title>JavaScript Text</title>
<script type="text/javascript">
function toggleAll(all){
if(all.checked)
selectAll(true);
else
selectAll(false);
}

function selectAll(bool){
var form = document.Form1.elements;
for(i = 1; i < form.length; i++){
if(bool)
form[i].checked = false;

form[i].disabled = bool;
}
}
</script>
</head>
<body>
<form name="Form1">
<input type="checkbox" name="chkAll" onclick="toggleAll(this)">All<br />
<input type="checkbox" name="chkList[]" value="1">1<br />
<input type="checkbox" name="chkList[]" value="1">2<br />
<input type="checkbox" name="chkList[]" value="1">3<br />
<input type="checkbox" name="chkList[]" value="1">4<br />
<input type="checkbox" name="chkList[]" value="1">5<br />
</form>
</body>
</html>
[/code]
Copy linkTweet thisAlerts:
@KorApr 05.2011 — You should think otherwise, and try to group the elements following a repeatable pattern, if possible regardless the id of the elements, if those ids don't follow a certain pattern. And they don't, in your case.

What I found as a common pattern is that both the "select all" checkbox and the rest of the checkboxes (and no other from other groups), are in the same container DIV and the "select all" checkbox is placed at the end. If so, the solution is simple:

<i>
</i>&lt;script type="text/javascript"&gt;
function ckUnck(obj){
var allC=obj.parentNode.getElementsByTagName('input');
for(var i=0;i&lt;allC.length-1;i++){
if(obj.checked){
allC[i].checked=!obj.checked;
allC[i].setAttribute('disabled','disabled');
}
else{
allC[i].removeAttribute('disabled');
}
}
}

and:
<i>
</i>&lt;input type="checkbox" value="32768" name="group[1][32768]" id="mce-group[1]-1-8" onclick="ckUnck(this);"

You don't need JQuery for that simple matter.
×

Success!

Help @scjava 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 6.13,
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: @meenaratha,
tipped: article
amount: 1000 SATS,

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

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