I am developing a shopping cart using codeigniter. In my cart page I need to update my cart on change in quantity of the item. I am getting the results with the help of button click. But my issue is that I need to get the same result on my key up. For that on key up corresponding button should be clicked
View page
<td>
<?php echo form_input(array('name' => $items['rowid']."_qty",'id' => $items['rowid']."_qty", 'value' => $items['qty'], 'maxlength' => '3', 'size' => '5')); ?>
<button class="btn btn-success " id= "<?php echo $items['rowid']."_qty" ?>" onclick="update_cart('<?=$items['rowid'];?>')" >update</button>
<script>
$("#<?php echo $items['rowid']."_qty" ?>").keyup(function(event){
$("#<?php echo $items['rowid']."_qty" ?>").click();
});
</script>
</td>
Script for update cart
function update_cart(itemid)
{
var qty= document.getElementById(itemid+"_qty").value;
$.ajax({
type: 'POST',
url: '<?php echo site_url("ajax_controller1/update_cart/'+itemid+'/'+qty+'")?>',
data: { id:itemid },
success:function(response){
$("#shoppingcart_container").html(response);
$(".total").click();
}
});
}
I just need to click currosponding button on change in quantity input field This is my Demo URL My website link
No need to add extra code to trigger click event. You can directly put update_cart
with your form_input
as
<?php
echo form_input(array(
'name' => $items['rowid']."_qty",
'id' => $items['rowid']."_qty",
'value' => $items['qty'],
'maxlength' => '3',
'size' => '5',
'onkeyup' => "update_cart('".$items['rowid']."')"
)
);
?>
Replace <td>
tag with above code and try
Check this
$("#id_of_field").keyup(function(event){
$("#id_of_button").click();
});
You can simply add onkeyup
property to your input element generation. Sorry, I'm not that fluent in php but you can use the same way you add other properties in: form_input(array( ..))
function update_cart(itemid, quantity)
{
console.log(quantity);
}
<input type="text" onkeyup="update_cart('someItemId', this.value)" />
</div>
here is the code so your problem can be solved :
first you need to give a common class to input for getting value and id of input :
$('.commonclass').on('input',function(e){
var id = $(this).attr("id");
update_cart(id)
});
function update_cart(itemid)
{
var qty= document.getElementById(itemid+"_qty").value;
$.ajax({
type: 'POST',
url: '<?php echo site_url("ajax_controller1/update_cart/'+itemid+'/'+qty+'")?>',
data: { id:itemid },
success:function(response){
$("#shoppingcart_container").html(response);
$(".total").click();
}
});
}
<td>
<?php echo form_input(array('name' => $items['rowid']."_qty",'id' => $items['rowid']."_qty",'onkeyup'=>'clickBtn("<?php echo $items['rowid']; ?>")', 'value' => $items['qty'], 'maxlength' => '3', 'size' => '5')); ?>
<button class="btn btn-success " id= "updateBtn<?php echo $items['rowid']; ?>" onclick="update_cart('<?=$items['rowid'];?>')" >update</button>
<script>
function clickBtn(id){
$("#updateBtn"+id).click();
}
</script>
</td>
This is the easiset way
To call any event you only need to call trigger function and pass event name as parameter.
$(".total").trigger("click");