Reputation: 542
I'm trying to get some specific title text to display via JavaScript, but I'm having some issues getting the entire string to show up.
The text I'm trying to display:
mechanical : Failed to copy
And here's what shows up in HTML:
`<td title="mechanical" :="" failed="" to="" copy="">mechanical : Failed to copy</td>`
The actual title displayed afterwards is just mechanical
.
In Javascript:
var copyResult = json_obj[i].CopyResult; //variable that contains the text
copyResult = copyResult.replace(/["{}]/g, " "); //regex that removes some characters and replaces them with spaces
The copyResult
variable is then added to the element I want.
It looks like having spaces "ends" the title attribute, so the browser tries to make more attributes with the remaining text.
What's the best way to fix this?
Upvotes: 2
Views: 4244
Reputation: 542
I was able to create a workaround. Since any space would end the title attribute, I simply used a regex to properly escape all of the space characters for the copyResult
variable.
var copyResult = copyResult.replace(/[ ]/g,"\u00a0")
\u00a0
is the Unicode character for NO-BREAK-SPACE
.
Upvotes: 4
Reputation: 1549
it's not the spaces ending the atribute, its the quotation marks... try escaping them with backslashes like \"
Upvotes: 1