Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript is taking only the first word in a sentence

am stuck over a small issue here.

I want to set the value of a input using JavaScript but jQuery is not displaying the full sentence.

var name = "Richard Keep";

But when I use try to do this

$('input#nameid').val(name)

to this input

<input type="text" id="nameid">

the value is set to Richard alone instead of Richard Keep. How do I make the jQuery echo the entire string and not just the first word in a string? Thanks

EDIT:

<td id="To_be_collected_port" onclick="requestPopup()" class="tr-selected">
   <div class="pop-td To_be_collected_port">
   </div>
   this is the content am fetching
</td>

This will display this only.

var str = $('#To_be_collected_port').text();

then

$('.xyz').html("<input type=\"text\" name=\"text\" id=\"selected-service-text\" value="+str+">");
like image 701
Richie Avatar asked Feb 06 '15 10:02

Richie


People also ask

How do you check if a word is present in a sentence using JavaScript?

The includes() method returns true if a string contains a specified string. Otherwise it returns false . The includes() method is case sensitive.

How do you turn a sentence into an array of words in JavaScript?

The split() method splits a string into an array of substrings. The split() method returns the new array. The split() method does not change the original string. If (" ") is used as separator, the string is split between words.

What does $() mean in JavaScript?

Usually when you encounter $() , that means the developer is using a javascript library, such as jQuery. The $ symbol is the namespace for those libraries. All the functions they define begin with $. , such as $. get() .

How do I remove the first word in JavaScript?

To remove the first word from a string, call the indexOf() method to get the index of the first space in the string. Then use the substring() method to get a portion of the string, with the first word removed. Copied!


1 Answers

You're missing escaped double quotes around the value attribute:

value="+str+"

should be:

value=\""+str+"\"

Fiddle - works now

Explanation of why it was displaying "this"

This means your input tag is effectively composing to this:

<input type="text" ... value=this is the content am fetching>
  • value = "this"
  • is, the, content, am, fetching = attributes without any value
like image 161
Josh Harrison Avatar answered Sep 28 '22 12:09

Josh Harrison