Siddhata Kadam Siddhata Kadam - 6 months ago 18
Javascript Question

how to use hide show in while loop with different ids

its my html

while($row=mysql_fetch_array($result_pag_data)){
$ad++;
$sql1=mysql_query("select *from company where com_id='$row[com_id]'");
$row1=mysql_fetch_array($sql1);

?>
<tr>
<input type="hidden" id="comid" name="comid" value="<?php echo $row1[com_id];?>"/>
<tr>
<td><img src="images/mobile.png" width="18" height="18" border="0" alt="Mobile"></td>
<td class="comm-details"><strong>Mobile</strong></td>
<td align="center">:</td>
<td class="comm-details"><?php if($row1['mobile1']!='') { ?> <a id="showmobile<?php echo $row1[com_id];?>">View Mobile Number</a> <span id="shwmb<?php echo $row1[com_id];?>" style="display:none"><?php echo $row1['mobile1'];}else{ echo 'Not Available';}?></td>
</tr>
}


my js

<script>
$(function (){
var comid = $('#comid').val();
$("#showmobile" + comid).click(function(){
$("#shwmb" + comid).show();
$("#shwmb" + comid).hide();
});
});
</script>


now I want to show the number when customer click on view mobile number....bt its work only on 1st viewd company...not for other company.....

nd when I use class instead of id ..then on one company view number click...it shows all companies number...

so now I use company id with element id name...bt its nt work properly....give me solution ...

Answer

Change your code as like below:

<?php
$ad = 0;
while ($row = mysql_fetch_array($result_pag_data)) {
    $ad++;
    $sql1 = mysql_query("select *from company where com_id='$row[com_id]'");
    $row1 = mysql_fetch_array($sql1);
    ?>
    <tr>
    <input type="hidden" id="comid" name="comid" value="<?php echo $row1[com_id]; ?>"/>
    <tr>
        <td><img src="images/mobile.png" width="18" height="18" border="0" alt="Mobile"></td>
        <td class="comm-details"><strong>Mobile</strong></td>
        <td align="center">:</td>
        <td class="comm-details">
            <?php if ($row1['mobile1'] != '') { ?> 
            <a id="showmobile<?php echo $row1[com_id]; ?>" onclick="return showmobile(<?php echo $row1[com_id]; ?>)">View Mobile Number</a> 
                <span id="shwmb<?php echo $row1[com_id]; ?>" style="display:none">
                    <?php echo $row1['mobile1']; ?>
                </span>
            <?php
            } else {
                echo 'Not Available';
            }
            ?>
        </td>
    </tr>
    <?php }
?>

<script>
    function showmobile(id) {
        if($("#shwmb" + id).css('display') == 'none') {
            $("#shwmb" + id).show();
        }else {
            $("#shwmb" + id).hide();
        }
    }
</script>