lundi 27 mars 2017

jQuery - remove value from input attribute

My jQuery script adds products IDs to an input type="hidden" value="id1,id2,id3"

Script is working and adds the ids on checkbox change, but if I uncheck the box, id must be remove from that input.

Part of my code:

<input type="hidden" value="100" class="productid100">

<script>

 var productid = jQuery(".productid<?php echo $_item->getId(); ?>").val();
 var defaultcombo = jQuery('.combodata').val();

 if(jQuery(this).is(":checked")) {

 jQuery('.combodata').attr("value", defaultcombo + productid+",");

 }else{

 var test = jQuery('#combo input:hidden[value=""]', productid+',').remove();
 console.log(test);

 }
</script>

<div id="combo">
<input type="hidden" class="hidden combodata" value="">
</div>

console.log()

Uncaught Error: Syntax error, unrecognized expression: 100,
at Function.ga.error (jquery-1.11.1.min.js:2)
at ga.tokenize (jquery-1.11.1.min.js:2)
at ga.select (jquery-1.11.1.min.js:2)
at Function.ga [as find] (jquery-1.11.1.min.js:2)
at m.fn.init.find (jquery-1.11.1.min.js:2)
at m.fn.init (jquery-1.11.1.min.js:2)
at new m (jquery-1.11.1.min.js:2)
at m.fn.init (jquery-1.11.1.min.js:2)
at m (jquery-1.11.1.min.js:2)
at HTMLInputElement.<anonymous> ...

This code is inside a PHP foreach for each product show in page.

At final I just need to add product id on checkbox click and remove the specify id on uncheck.

Problem is here:

jQuery('#combo input:hidden[value=""]', productid+',').remove();




Aucun commentaire:

Enregistrer un commentaire