Forum Moderators: open

Message Too Old, No Replies

Changing array and class values onclick

         

AssertiveWD

5:43 pm on Dec 31, 2007 (gmt 0)

10+ Year Member



I am trying to make a substitute for checkboxes using javascript and php. I am able to pass the variables, but the problem I have is changing the array values and class values depending on if it is selected or not.

Heres the code I threw together. It makes sense to me but I can't seem to get it to work.


<style type="text/css">

#navigationDiv a {
text-decoration:none;
display:block;
color:black;
margin-bottom:5px;
}

#navigationDiv a.selected {
text-decoration:underline;
}

#navigationDiv a.unselected {
text-decoration:none;
}

</style>

<script type="text/javascript">
var check_fields = new Array();
check_fields[0] = "0";
check_fields[1] = "0";
check_fields[2] = "0";
check_fields[3] = "0";
check_fields[4] = "0";
check_fields[5] = "0";
check_fields[6] = "0";
check_fields[7] = "0";

function checked(obj, chk) {

if (check_fields[chk] = 1){
check_fields[chk] = 0;
obj.className = "unselected";
}

else {
check_fields[chk] = 1;
obj.className = "selected";
}

}

</script>

<div id="navigationDiv">
<a href="#" onclick="checked(this, 0)" class="">anchor1</a>
<a href="#" onclick="checked(this, 1)" class="">anchor2</a>
<a href="#" onclick="checked(this, 2)" class="">anchor3</a>
<a href="#" onclick="checked(this, 3)" class="">anchor4</a>
<a href="#" onclick="checked(this, 4)" class="">anchor5</a>
<a href="#" onclick="checked(this, 5)" class="">anchor6</a>
<a href="#" onclick="checked(this, 6)" class="">anchor7</a>
<a href="#" onclick="checked(this, 7)" class="">anchor8</a>
</div>

Thanks for the help

penders

12:29 pm on Jan 2, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



if (check_fields[chk] = 1) {
check_fields[chk] = 0;
obj.className = "unselected";
}

You are assigning(=) the value in your IF statement, as opposed to comparing equality(==). So this will always avaluate to true; it will always be 'unselected'.