In the head of my script, I define a click function for the class ActiveQuestionCycler, which cycles through a series a panels. (Full code for this function at the bottom of this post.) When I assign that class to a div element (e.g. <div class="ActiveQuestionCycler"></div>), the function works, so there are no errors in this code.
In addition, I can successfully use the following code to add the class activequestioncycler to a new div element (<div class="NewDiv"></div>) so that it, too, runs the activequestioncycler function when I click on it.
<script type='text/javascript'>
$(document).ready(function(){
$(".NewDiv").addClass("ActiveQuestionCycler");
});
</script>
However, I am having trouble adding the class to one div element (e.g. <div class="DivToClick"></div>) when another (e.g. <div class="DivToAdoptClass"></div>) is clicked.
Here's the code that isn't working:
<script type='text/javascript'>
$(document).ready(function(){
$(".DivToClick").click(function () {
$(".DivToAdoptClass").addClass("ActiveQuestionCycler");
});
</script>
If I click DivToClick it should add the ActiveQuestionCycler class to DivToAdoptClass so that, if I subsequently click DivToAdoptClass, it will run the function and show the panels. However, nothing happens.
If I use something other than addClass, the function above works fine. For example, the following function successfully hides DivToAdoptClass when I click DivToClick
<script type='text/javascript'>
$(document).ready(function(){
$(".DivToClick").click(function () {
$(".DivToAdoptClass").hide();
});
</script>
If someone could help explain why addClass doesn't work when it runs within a click function, I would appreciate it.
Thanks!
JUST IN CASE: CODE THAT DEFINES CLICK FUNCTION FOR ACTIVEQUESTIONCYCLER CLASS
<script type="text/javascript">
$(document).ready(function () {
var controlPanel = []
var questiontype= '<?php echo $data['QuestionType']; ?>'
var panels=$("."+questiontype+"Question."+questionlevel+".active").length;
$(".ActiveQuestionCycler").click(function () {
var rand = Math.floor(Math.random()*panels);
if (controlPanel.length >= $("."+questiontype+"Question."+questionlevel+".active").length) {
controlPanel.length = 0
}
while (controlPanel.indexOf(rand) !== -1){
rand = Math.floor(Math.random()*panels);
}
controlPanel.push(rand)
for(i=1;i<=panels;i++){
if($("."+questiontype+"Question."+questionlevel+".active").is(":visible")){
$("."+questiontype+"Question."+questionlevel+".active").hide();
}
}
setTimeout(function(){
$("."+questiontype+"Question."+questionlevel+".active").eq(rand).show();
});
});
</script>