vellai durai vellai durai - 4 months ago 36
PHP Question

How to change value of textbox onchange created using loop in jquery

I need to change the value of second one on change of first one

for ex if the user change the value for valuex00 the same value should be in valuey00, same for valuex01 and valuey01

//first one
<?php $a=0;?>
<?php for ($i=0;$i<5;$i++) {?>
<?php for ($j=0;$j<5;$j++) {?>

<input type="text" name="<?php echo 'post1['.$a.'][value1]'?>" class="valuex<?php echo $i.$j;?>">


<?php $a++;?>
<?php }?>
<?php }?>


//second one
<?php $a=0;?>
<?php for ($i=0;$i<5;$i++) {?>
<?php for ($j=0;$j<5;$j++) {?>

<input type="text" name="<?php echo 'post2['.$a.'][value1]'?>" class="valuey<?php echo $i.$j;?>">


<?php $a++;?>
<?php }?>
<?php }?>


How to achieve this using jquery on change event

Please suggest with solution

Answer

Try this

<!DOCTYPE html>
<html>
<body>
<?php $a=0;?>
<?php  for ($i=0;$i<5;$i++) {?>
<?php  for ($j=0;$j<5;$j++) {?>

    <input type="text" onchange="myFunction(this)" name="<?php echo 'post1['.$a.'][value1]'; ?>" id="valuex_<?php echo $i.$j;?>" class="valuex<?php echo $i.$j;?>">


<?php $a++;?>
<?php }?>
<?php }?>

    <br>
    //second one      
   <?php $a=0;?>
<?php  for ($i=0;$i<5;$i++) {?>
<?php  for ($j=0;$j<5;$j++) {?>

    <input type="text" name="<?php echo 'post2['.$a.'][value1]'?>" id="valuey_<?php echo $i.$j;?>"  class="valuey<?php echo $i.$j;?>">


<?php $a++;?>
<?php }?>
<?php }?>
<script>
function myFunction(ele) {
    var id = ele.id;
    var valuefirstone = document.getElementById(id).value;
    var res = id.split("_");
    document.getElementById("valuey_"+res[1]).value = valuefirstone;
}
</script>
</body>
</html>

My result

enter image description here