Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Losing text after a space setting the value in a new field

I am having an odd issue when using Javascript to create a new field. I have button that, when clicked, will create two text fields. I give those text fields a string as a value. However, it is only working correctly if I give it a string with no spaces. If my string contains a space then it makes the value equal to the text before the space occurs.

function nameField() {
  $('#nameArea').html("<br/>First Name:<input type='text' id='fnField' value=" + $('#firstName').text() + ">Last Name:<input type='text' id='lnField' value=" + $('#lastName').text() + "></input><button id='bName' onclick='updateName(fnField.value, lnField.value)'>Save Changes</button>");
  $('#firstName').html("");
  $('#lastName').html(""); 
}

So for example, if the firstName value is David E, it is currently only putting David in the field as the value. However, I have used alert and can confirm that $('#firstName').text(); does contain the full David E. How can I make sure that the text after the space doesn't get slashed out?

like image 954
user3225440 Avatar asked Nov 23 '15 08:11

user3225440


People also ask

How do I remove spaces from a text field?

Approach 1: We can remove space between any two tags by simply using margin-bottom property. The margin-bottom property sets the bottom margin of an element. We will assign a negative value to the margin-bottom of a particular tag to eliminate the extra space between the tags as shown.

How do you put a space in a text field?

To add space inside a form's text field, use the CSS padding property.

How can remove space from textbox using jQuery?

Answer: Use the jQuery $. trim() function You can use the jQuery $. trim() function to remove all the spaces (including non-breaking spaces), newlines, and tabs from the beginning and end of the specified string.


2 Answers

The issue is because you don't have any string delimiters around the value. Note the double quotes (") around the values of the attributes in this example:

$('#nameArea').html('<br/>First Name:<input type="text" id="fnField" value="' + $('#firstName').text() + '">Last Name:<input type="text" id="lnField" value="' + $('#lastName').text() + '"></input><button id="bName" onclick="updateName(fnField.value, lnField.value)">Save Changes</button>');

Alternatively you can avoid the string concatenation completely by using a Template Literal, but be aware this is unsupported in IE.

$('#nameArea').html(`<br/>First Name:<input type="text" id="fnField" value="${$('#firstName').text()}">Last Name:<input type="text" id="lnField" value="${$('#lastName').text()}"></input><button id="bName" onclick="updateName(fnField.value, lnField.value)">Save Changes</button>`);

Finally, you can tidy this HTML further by removing the onclick attribute and using unobtrusive event handlers instead. You can use data attributes to contain the necessary metadata with the element.

like image 127
Rory McCrossan Avatar answered Sep 28 '22 08:09

Rory McCrossan


This is a logical error caused by the lack of quotes around the value you give.

You set the html to value=First Lastname instead of value='First Lastname'. In the first case most browsers parse the string till the whitespace and therefor only the firstname shows up.

Try

$('#nameArea').html("<br/>First Name:<input type='text' id='fnField' value='" + $('#firstName').text() + "'>Last Name:<input type='text' id='lnField' value='" + $('#lastName').text() + "'></input><button id='bName' onclick='updateName(fnField.value, lnField.value)'>Save Changes</button>");
like image 32
Bas van Stein Avatar answered Sep 28 '22 07:09

Bas van Stein