user2128056
user2128056

Reputation: 111

Allow whitespace

So, if I was to add something in my input with a space(test test)and hit submit, it would show it as test+test also if i double hit submit, it shows it test%2Btest instead of test+test Need help with those things..

<form name="input" action="" method="get">
Search: <input type="text" name="search">
<input type="submit" value="Submit">
  <div id="result"></div>
</form>
$('form').submit(function() {
    var form_data = ($(this).serialize());
    window.location.hash = form_data.replace('=','/');
    return false;
});

$(window).on('hashchange', updateVal);

updateVal();

function updateVal() {
   var values = window.location.hash.slice(1).split('/');
   $("[name=" + values[0] + "]").val(decodeURIComponent(values[1]));
}

Upvotes: 0

Views: 175

Answers (1)

Vadim
Vadim

Reputation: 8779

You just need to replace + to space each time you set a value of input:

$("[name=" + values[0] + "]").val(decodeURIComponent((values[1] || '').replace(/\+/g, ' ')));

Working example you can see here: http://jsbin.com/elewux/2/edit

Upvotes: 1

Related Questions