hrishi hrishi - 11 days ago 6
HTML Question

Append to HTML ignores content after space

I am appending input type using java script. but it is removing content after blank space

Java script code is



var temp = '<input type="hidden" name="nearest[]" id="nearest" value='+distance+'_'+location+'>';
$('#nearest_div').append(temp);

<div id="nearest_div"></div>





But if variable location is having space in name then it dont get append properly
for eg if location = United States then it appends United only and ignores content after space

Ali Ali
Answer

You didn't quote Value, that's why it is not working.

For example if the value is "The United States of America", your code will produce a tag like this:

<input type="hidden" name="nearest[]" id="nearest" value=The United States>

but it should be:

<input type="hidden" name="nearest[]" id="nearest" value="The United States">

I changed your code:

  var temp = '<input type="hidden" name="nearest[]" id="nearest" value="'+distance+'_'+location+'>"';
  $('#nearest_div').append(temp);