Daniel D
Daniel D

Reputation: 3677

using javascript to create an HTML form - text input with spaces

I'm using javascript (backbone) to fill out an HTML form template. The form is an edit form, so it will be populated with existing values.

Unfortunately, when I browse to the form, values for a text input are getting truncated. For example, say the value of the field should be populated with "hello world" - but the form only shows the word before the space (hello) and the actual source looks like this (notice it thinks world is an attribute):

<input type="text" name="name" id="name" value="hello" world="">`

I'm not a huge HTML/Javascript expert. Does anyone know how I can escape the input?

Here's the template text that generates that form:

I'm not a huge HTML/Javascript expert. Does anyone know how I can escape the input?

Here's the template text that generates that form:

 <h1>Edit activity</h1>
  <form id="edit-activity" name="activity">
   <div class="field">
    <label for="name"> name:</label>
    <input type="text" name="name" id="name" value=<%= name %> >
   </div>
   <div class="actions">
     <input type="submit" value="Update Activity" />
   </div>
  </form>
  <a href="#/index">Back</a>

Upvotes: 0

Views: 525

Answers (1)

Joseph Marikle
Joseph Marikle

Reputation: 78520

<input type="text" name="name" id="name" value=<%= name %> >

Your problem is here. Try this instead

<input type="text" name="name" id="name" value="<%= name %>" >

What's happening is you are basically making this markup:

<input type="text" name="name" id="name" value=hello world >

Without the "s, the browser is interpreting the world value as an attribute. Placing the quotes in there does not adversely affect it, and when you think about it, it makes sense why it would work that way.

Upvotes: 1

Related Questions