Ramnath
Ramnath

Reputation: 438

Text box showing  

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

Answers (5)

craig1231
craig1231

Reputation: 3867

What function(s) are you using to get the info from the database?

If "My&nbsp; 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

Jim Jose
Jim Jose

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&nbsp; Name') )

If you don't want to use the jquery method, you can find some custom htmlDecode method like,

JavaScript html_entity_decode

Upvotes: 0

Tomalak
Tomalak

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

javadeveloper
javadeveloper

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

Dave Newton
Dave Newton

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

Related Questions