AlexGH AlexGH - 19 days ago 7
Javascript Question

Get JQuery data- that contains value with spaces

I have two functions: in the first one, I'm setting the

data-file_name
attribute using JQuery:

"<input type=\"image\" + " data-file_name=" + result.fileName/>"


Which works great, but when I'm trying to retrieve that value from the second function if the
data-file_name value
contains spaces I just get the first word(for example if
data-file_name= "The book blue"
I just get
The
, if
data-file_name = "Red Dragon"
I just get on the second function
Red
it stops when it find an space... )
This is the way I'm retrieving the value in the second function:

var fileName = $(this).data("file_name");


So
fileName variable
will not get the real value because it doesn't include anything behind the spaces, which way should I use to retrieve the value complete including spaces and everything?

Answer

There is a concatenation problem.

Replace with this

"<input type=\"image\" data-file_name=\"" + result.fileName + "\" />"
Comments