Skip to content Skip to sidebar Skip to footer

Dynamic Input Name With Double Quotes Issue

Scenario : When
text has double quote. JQuery method to append dynamic input and to alert the value of input is not working. I know this issue trigger when escaping quo

Solution 1:

If you must use double quotes, you can remove them like so:

var divText = $("div").text().replace(/"/g, '');

Fiddle

Solution 2:

Playing off the reg exp from the jQuery docs

var divText = $("div").text();
var name = divText.replace(/("|:|\.|\[|\]|,)/g, "\\$1");
$('form:not(:has([name="' + name + '"]))').append($('<input>', {
  type: 'text',
  value: divText,
  name: divText
}));
alert($('[name="' + divText + '"]').val());
<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><div>"</div><form></form>

Post a Comment for "Dynamic Input Name With Double Quotes Issue"