Chris
Chris

Reputation: 8826

Escaping + character when read to passed as ajax data (to prevent concatenation)

I'm reading the value of an input text field and passing it to be used as ajax data

The field value has a +

<input name="someval" type="text" value="Receive (+ open)" />

and looks like when parsed with data, it parses the + as a jquery concatenation.

data: 'someval=' + $("input[name=someval]").val(),

This is the first time I notice this behavior.

Thanks

Upvotes: 0

Views: 244

Answers (1)

Roatin Marth
Roatin Marth

Reputation: 24085

Try encodeURIComponent:

'someval=' + encodeURIComponent($("input[name=someval]").val())

Better yet, let jQuery handle it for you:

data: { someval:$("input[name=someval]").val() }

jQuery will automatically escape your values (and keys) into the correct format (using jQuery.param()) for the data type (eg "application/x-www-form-urlencoded").

Upvotes: 2

Related Questions