Hai Truong IT
Hai Truong IT

Reputation: 4187

Error when get ID in jquery

I have a code php:

<?php
for ($i=0, $n=count( $this->item ); $i < $n; $i++) {
$row = &$this->item[$i];
?>
<tr id="<?php echo $row->id; ?>" class="row">
<span id="name_<?php echo $row->id; ?>" class="text">&nbsp;<?php echo $row->name; ?></span>
<input type="text" value="<?php echo $row->name; ?>" class="editbox" id="name_input_<?php echo $row->id; ?>" />
</tr>
<?php
$i++;
}
?>

and Jquery get id from this script php

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    $('.row').click(function(){
       var ID = $(this).attr('id');
       $('#name_'+ID).hide(); 
       $('#name_input_'+ID).show();
       alert(ID); 
    });
});
</script>

Error when i output alert(ID) result is NULL, How to get ID ?

Upvotes: 1

Views: 256

Answers (3)

Laurence Tuck
Laurence Tuck

Reputation: 420

you may be having issues with the main div ID itself - it should not start with a number http://www.w3schools.com/tags/att_standard_id.asp - rather use a name with the ID suffix and then remove the prefix to get the number value.

try this on the tr: <tr id="rowid_<?php echo $row->id; ?>" class="row">

and then in your code:

    $('.row').click(function(){
           var rowid = $(this).attr('id');
var ID = rowid.replace(/rowid_/,'');
           $('#name_'+ID).hide(); 
           $('#name_input_'+ID).show();
           alert(ID); 
        });

Upvotes: 0

ime.devdesks
ime.devdesks

Reputation: 394

Try this....

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"</script>
<script type="text/javascript">
$(document).ready(function(){
$('.row').click(function(){
   var ID = $(this).closest('tr').attr('id');
   $('#name_'+ID).hide(); 
   $('#name_input_'+ID).show();
   alert(ID); 
  });
});
</script>

Upvotes: 1

shox
shox

Reputation: 1160

i think the problem in your html code table must look like this :

<table>
  <tr> 
   <td></td>
  </tr>
</table>

change to :

<?php
for ($i=0, $n=count( $this->item ); $i < $n; $i++) {
$row = &$this->item[$i];
?>
<tr id="<?php echo $row->id; ?>" class="row">
<td>
<span id="name_<?php echo $row->id; ?>" class="text">&nbsp;<?php echo $row->name; ?></span>
<input type="text" value="<?php echo $row->name; ?>" class="editbox" id="name_input_<?php echo $row->id; ?>" />
</td>
</tr>
<?php
$i++;
}
?>

Upvotes: 1

Related Questions