1

I want to pass the id to the next page on clicking the entire row. I tried to do it myself but I wasn't able to do so . my code is below :

$( "#tablerow" ).click(function() {
  var jobvalue=$("#jobid").val();
  alert(jobvalue);
  window.location.href = "jobsview.php?id=" + jobvalue;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tbody>
   <tr id="tablerow">
    <td><?=$srno?></td>
    <td id="jobid"><?=$row['ID']?></td>
   </tr>
  </tbody>
</table>

1 Answer 1

5

val() method works for form elements like input, select etc.

Use text() method,

var jobvalue = $("#jobid").text();

Update

An HTML can have only one ID throughout the document. To enable click event for multiple elements and pass the one that is clicked onto another page, change ID attribute to class.

<table>
  <tbody>
    <tr class="tablerow" >
     <td><?=$srno?></td>
     <td class="jobid"><?=$row['ID']?></td>
    </tr>
  </tbody>
</table>

Then you can get the once clicked in JS as follows,

$( ".tablerow" ).click(function() {
   /**  $(this) will refer to current tablerow clicked
     *  .find(".jobid") will find element with class `jobid`
     *  inside currently clicked tablerow
   */
   var jobvalue = $(this).find(".jobid").text();
   alert(jobvalue);
   window.location.href = "jobsview.php?id=" + jobvalue;
});
Sign up to request clarification or add additional context in comments.

7 Comments

But when i am using while loop it only works for first row.Can you help me solve the problem too.
Need sample structure with multiple values, and how it's been passed in JS under location.href
<?php while ($row = oci_fetch_array ($sqll, OCI_ASSOC)) { ?> <td id="jobid"><a><?php echo $t = $row['EMPLJ_JOB_ID'] ?></a> </td> <?php } ?>
' <script> $(document).ready(function(){ $( "#tablerow" ).click(function() { var jobvalue=$("#jobid").text(); alert(jobvalue); }); }); </script>'
Do you want to pass all IDs at once to another page comma separated?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.