Reputation: 438
We are displaying some text in text box coming from database. We set this value in text box using one Js function. Problem is that the string from DB is "My Name"
. In textbox it should be displayed as "My Name"
, but it gets displayed as "My Name"
.
How to solve this problem?
My HTML for text box is as follows,
<input type="text" class="textfield" id="searchText">
Note : Can't change the value returned from DB. Using javascript->java->ORACLE to pull value from the DB.
Upvotes: 2
Views: 9244
Reputation: 3867
What function(s) are you using to get the info from the database?
If "My Name"
is in the database, then you would have to strip the HTML tags.
If "My Name"
is in the database, then you should remove functions like html_entity_decode
.
Upvotes: 0
Reputation: 1319
You just need to do htmldeDecode
before you assign the value.
function htmlDecode(value){
return $('<div/>').html(value).text();
}
$('#searchText').val( htmldeDecode('My Name') )
If you don't want to use the jquery method, you can find some custom htmlDecode
method like,
Upvotes: 0
Reputation: 338148
$("#searchText").val( $("<div>").html(database.data).text() );
This works by creating a temporary element and filling its .html()
with your database string. Then the raw .text()
is extracted from that and passed to your input element. Any HTML entity will be replaced by the corresponding character.
This is an ugly hack. I strongly suggest fixing your database values instead.
Upvotes: 1
Reputation: 793
Textbox cannot understand HTML formatting. Have you heard of GIGO - Garbage In Garbage Out? Make sure the db holds the right values.
Other hack could be to use String parsing function to substitute with a space.
Upvotes: 1
Reputation: 160181
You need to decode the string in JavaScript, if you're unable to modify it on the server-side.
var foo = Encoder.htmlDecode(stringValueFromDb);
The problem is that if the DB expects values to be HTML-encoded, you'll need to do that somewhere too, unless it already happens server-side.
Upvotes: 1