Question

I have a variable, PRODUCT_CUSTOM_1_ and PRODUCT_NAME_ . This is suppose to grab the product options from the html that is being displayed. We have a few products that contain quotations, " and '. When we grab these strings and post them elsewhere, it cuts off at the ". I either need the " removed or included within us posting it elsewhere.

How can I remove or include the " and ' from the two variables into my hidden form iwantCheckoutForm?

An example of a product name: 1/8" x 40' roll of tape An example of the custom option: 1/8" black, 1/4" red

var BongoCheckout = {insertForm: function() {

var custom =new Array;
for(i=0;i< qtys.length ;i++){
        custom[i]="";
        for(j=0;j< $($("td.ys_itemInfo")[i]).children().children().length; j++){
             custom[i]= custom[i]+"   "+$($($("td.ys_itemInfo")[i]).children().children()[j]).text();
        }

$("#iwantCheckoutForm").append('<input type="hidden" name="PRODUCT_ID_'+(i+1)+'" value="'+codes[i]+'">');
$("#iwantCheckoutForm").append('<input type="hidden" name="PRODUCT_NAME_'+(i+1)+'" value="'+items[i]+'">');
$("#iwantCheckoutForm").append('<input type="hidden" name="PRODUCT_PRICE_'+(i+1)+'" value="'+price[i]+'">');
$("#iwantCheckoutForm").append('<input type="hidden" name="PRODUCT_Q_'+(i+1)+'" value="'+qtys[i]+'">');
$("#iwantCheckoutForm").append('<input type="hidden" name="PRODUCT_CUSTOM_1_'+(i+1)+'" value="'+custom[i]+'" /> ');

if (per_item_shipping) {
$('form[name="iwantCheckoutForm"]').append('<input type="hidden" name="PRODUCT_SHIPPING_'+(i+1)+'" value="'+shipping_cost.toString()+'"> ');
} else {
$('form[name="iwantCheckoutForm"]').append('<input type="hidden" name="PRODUCT_SHIPPING_'+(i+1)+'" value="'+shipping_cost_breakdown+'"> ');
}
}}

I know that this is a duplicate question sort of, but i couldn't quite figure out how to implement the 10+ suggestions into my current script. I just can't seem to include the regex argument, without the script from breaking. I tried to add it within the for loop.

        for(j=0;j< $($("td.ys_itemInfo")[i]).children().children().length; j++){
             custom[i]= custom[i]+"   "+$($($("td.ys_itemInfo")[i]).children().children()[j]).text(custom.replace(/\"/g, ""));
        }

Blame that on me still being a n00b, and blame that on my ADD. I suppose I just don't know where I need to add .replace(/\"/g, "") as this code seems to be working for others.

Was it helpful?

Solution

Instead of creating HTML code for an element, just create the element and set the value. That elliminates all problems with escaping characters:

$("#iwantCheckoutForm").append(
  $('<input>', { type: 'hidden', name: 'PRODUCT_NAME_'+(i+1) }).val(items[i])
);

OTHER TIPS

use function

function makeInput(name,value) {
 var tmp = $('<input type="hidden" />');
 tmp.attr('name', name);
 tmp.val(value);
 return tmp;
}

example: http://jsfiddle.net/5EX3y/2/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top