views:

44

answers:

2

I have two images yes and no, which change color when clicked, they get highlighted. When clicked on yes_unselect, it needs to change to yes_select and change the id of no to no_unselect. I am facing two problems.
1. When once clicked the yes_unselect is changing to yes_select but clicking on that again is not changing back to yes_unselect.
2. When yes_unselect changes to yes_select i want id="no" image, no_select to change to no_unselect.

<div id="yes">
<input type="image" src="images/yes_unselect.jpg" id="yes" onClick="if (this.src='images/yes_unselect.jpg') {this.src='images/yes_unselect.jpg';} else {if (this.src='images/yes_select.jpg') {this.src='images/yes_unselect.jpg';}}">
</div>

<div id="no">
<input type="image" src="images/no_select.jpg" id="no" onClick="if (this.src='images/no_select.jpg') {this.src='images/no_unselect.jpg';} else {if (this.src='images/no_select.jpg') {this.src='images/no_unselect.jpg';}}">
</div>
+2  A: 

How about something like this in the head:

var theAnswer = undefined; //Global variable for access later.
function changeAnswer(yes) {
    theAnswer = yes;
    //Yes should be a boolean, therefore true==yes, false==no
    var eYes = document.getElementById('yes'),
        eNo = document.getElementById('no');
    eYes.src = ( yes ) ? 'images/yes_select.jpg' : 'images/yes_unselect.jpg';
    eNo.src = ( yes ) ? 'images/no_unselect.jpg' : 'images/no_select.jpg';
}

Of course, you would have to change the conflict in ids. Multiple elements mustn't have the same id, so change the <div>s ids to something like yesDiv and noDiv.

Then the image's onclick can be changeAnswer(true); for the yes button and changeAnswer(false); for the no button. At any place in the script theAnswer can be accessed and will reflect the user's current choice.

clarkf
Thanks .. it works. How can i get to know which element is selected when i am trying to insert it into the database... i work in php and mysql. how can i grab this on the next page for insertion in DB?
Scorpion King
@Scorpion King - You need to somehow pass the data to your server. Presumably you are submitting a form, in which case you can modify the function above to set the value of a hidden `input` in the form to the value of the yes/no selector. If you aren't using forms you must get into XHR....
clarkf
yes i am using the form and in the next php i would be checking the value of yes. but i was wondering how am i going to input the hidden value of yes/no? can i use php inside the function in javascript?
Scorpion King
+1  A: 

Something like this:

<script type="text/javascript">

function swap() {
    var e_yes = document.getElementById("yes");
    var e_no = document.getElementById("no");
    var yes_unselect = 'images/yes_unselect.jpg';
    var yes_select = 'images/yes_select.jpg';
    var no_unselect = 'images/no_unselect.jpg';
    var no_select = 'images/no_select.jpg';

    var result = e_yes.src == yes_unselect;


    e_yes.src = result ? yes_select : yes_unselect;
    e_no.src = result ? no_unselect : no_select;
}


</script>

<div id="div_yes">
    <input type="image" src="images/yes_unselect.jpg" id="yes" onclick="swap()">
</div>
<div id="div_no">
    <input type="image" src="images/no_select.jpg" id="no" onclick="swap()">
</div>
some