CoreSupport
asked on
Show hide table rows onclick php javascript
Hi i have a page which displays FAQ's, these are generated from a database and a table row is created for each question and each answer, i want to be able to show/ hide each answer by clicking a button and calling javascript function
<script language="Javascript">
function showanswer()
{
document.getElementById('a nswer').st yle.visibi lity="visi ble";
document.getElementById('s how').styl e.visibili ty = "hidden";
document.getElementById('h ide').styl e.visibili ty = "visible";
}
function hideanswer()
{
document.getElementById('a nswer').st yle.visibi lity="hidd en";
document.getElementById('h ide').styl e.visibili ty = "hidden";
document.getElementById('s how').styl e.visibili ty = "visible";
}
</script>
//mssql query to get questions and answers from database
<table><?
While($row = mssql_fetch_object($result 2))
{
?>
<tr>
<td width="400"> <?echo $row->Question ?> </td>
<td>
<input type="button" value="Show" id="show" onclick="showanswer()" >
<input type="button" value="Hide" id="hide" style="visibility: hidden;" onclick="hideanswer()" >
</td>
</tr>
<tr>
<td style="visibility: hidden;" id="answer" colspan="2"><? echo $row->Answer?></td></tr>
<?}?>
This is working fine for the first answer, it can be displayed/ hidden using the buttons, but i cannot display/hide the rest of the answers
I think this is because each row being created for the answers inside the while loop has the same id, as does each show button and each hide button.
and so i think i need to create some sort of array and use this as the id, any help on how to do this and then reference the id in the javascript functions?
or any other suggestions on how i should do this?
thanks
<script language="Javascript">
function showanswer()
{
document.getElementById('a
document.getElementById('s
document.getElementById('h
}
function hideanswer()
{
document.getElementById('a
document.getElementById('h
document.getElementById('s
}
</script>
//mssql query to get questions and answers from database
<table><?
While($row = mssql_fetch_object($result
{
?>
<tr>
<td width="400"> <?echo $row->Question ?> </td>
<td>
<input type="button" value="Show" id="show" onclick="showanswer()" >
<input type="button" value="Hide" id="hide" style="visibility: hidden;" onclick="hideanswer()" >
</td>
</tr>
<tr>
<td style="visibility: hidden;" id="answer" colspan="2"><? echo $row->Answer?></td></tr>
<?}?>
This is working fine for the first answer, it can be displayed/ hidden using the buttons, but i cannot display/hide the rest of the answers
I think this is because each row being created for the answers inside the while loop has the same id, as does each show button and each hide button.
and so i think i need to create some sort of array and use this as the id, any help on how to do this and then reference the id in the javascript functions?
or any other suggestions on how i should do this?
thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi leakim971
this is what i was looking for, but using 'the_id' parameter is not working
so how do i send the php variable $i to the javascript parameter ''the_id' so that it knows which answer to show/hide?
thanks
this is what i was looking for, but using 'the_id' parameter is not working
so how do i send the php variable $i to the javascript parameter ''the_id' so that it knows which answer to show/hide?
thanks
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
yes working great now, thankyou
You're welcome! Thanks a lot for the points!
Use the toggle effect on your item to show and hide it.
http://www.w3schools.com/jquery/jquery_effects.asp
http://www.w3schools.com/jquery/tryit.asp?filename=tryjquery_slide_toggle