0

I already fetched all values from database into table

foreach ( $result as $print )   {
    ?>
<form method="post">
    <tr>
        <td><a href="#" id="edit-btn">Edit</a></td>
</tr>
<tr>
<div class="edit-box1">
   <input id="idd" type="text" readonly="readonly" value="<?php echo $idd; ?>" name="status111">
   <input type="submit" value="GO" name="edit-go">
</div>
</tr>
</form>
}

And this is the jQuery code:

   jQuery(document).ready(
    function() {
        jQuery("#edit-btn").click(function() {
            jQuery("#idd").prop("readonly", false);
        });
    });

The problem is that I got 20 rows and my edit button just run on my first row.

How can I make my jQuery run on all rows ?

Ahmed Shamel
  • 1,982
  • 3
  • 24
  • 58
Hammad
  • 66
  • 1
  • 7

3 Answers3

1

Id cannot be used more than once, use class instead, try this code:

<?php foreach($result as $print): ?>
    <form method="post" class="form">
        <tr>
            <td><a href="#" class="edit-btn">Edit</a></td>
        </tr>
        <tr>
            <div class="edit-box1">
            <input class="idd" type="text" readonly="readonly" value="<?php echo $idd; ?>" name="status111">
            <input type="submit" value="GO" name="edit-go">
            </div>
        </tr>
    </form>
<?php endforeach; ?>
<script>
    jQuery(document).on('click','.edit-btn',function(){
        jQuery(this).closest('.form').find('.idd').prop('readonly',false);
    });
</script>
Rohit Khatri
  • 1,980
  • 3
  • 25
  • 45
1

This will be your HTML

$cnt=0;
foreach ( $result as $print )   {
?>
    <form method="post">
        <tr>
            <td><a href="#" class="edit-button" id='<?php echo $cnt;?>'>Edit</a></td>
        </tr>
        <tr>
        <div class="edit-box1">
            <input id="idd<?php echo $cnt;?>" type="text"  readonly="readonly" value="<?php echo $idd; ?>" name="status111">
            <input type="submit" value="GO" name="edit-go">
            <?php $cnt++; ?>
        </div>
        </tr>
     </form>
}

This will be your jquery code

<script>

   jQuery(document).ready(
       function() {
           jQuery(".edit-button").click(function() {
           jQuery("#idd"+$(this).attr("id")).prop("readonly", false);
       });
   });
</script>

Hope this will work surely.

Dhaval Purohit
  • 1,270
  • 10
  • 28
0

1 ) all id convert class

2) Use method removeAttr("readonly")

<tr>
    <td><a href="#" class="edit-btn">Edit</a></td>
</tr>

<input class="idd" type="text" readonly="readonly" value="<?php echo $idd; ?>" name="status111">

And you can use $(".idd").removeAttr("readonly")

 $(document).ready(function(){
           $(".edit-btn").click(function(){
              $(".idd").removeAttr("readonly");
               $(".idd:first").focus();

           })
       })

Example :

<html>
    <head>
        <meta charset="utf-8">
    </head>
    <body>
        <tr>
        <td><a href="#" class="edit-btn">Edit</a></td>
        </tr>
        <input type="text" class="idd" readonly>
        <input type="text" class="idd" readonly>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
        <script>
           $(document).ready(function(){
           $(".edit-btn").click(function(){
              $(".idd").removeAttr("readonly");
               $(".idd:first").focus();
               
           })
       })
        </script>
    </body>
</html>
Ehsan
  • 12,655
  • 3
  • 25
  • 44