1

This is an input box in whose value I have inserted a text which is working fine

$("#inputbox").val(insert_text);

Now, what I want further is to insert <html> tags inside .val(), such as

$("#inputbox").val("<mark>"+insert_text+"</mark>");

so that it appears in the value of inputbox

I want the value text to highlight, I tried to implement it the above way but it didn't work.

Please help me through it.

Abhi
  • 350
  • 1
  • 4
  • 13

3 Answers3

1

Input elements don't allow html markup in the value.

If you want to do this you'll need to change from an input element to an editable div. You can style these such that they look (and behave) the same as input elements. For the relevant styles check out:

How do I make an editable DIV look like a text field?

Community
  • 1
  • 1
ReallyMadeMeThink
  • 1,061
  • 7
  • 11
0

In short you cannot add html to a value to change the appearance of the input.

You can use some jquery code to add a class to the input if it is filled out after which you can style it with CSS.

JanR
  • 6,052
  • 3
  • 23
  • 30
0

If you want to change color of the text inside the text field, why don't you add CSS to input type, e.g.

JS:

$("#inputbox").val(insert_text);

$("#inputbox").css('color', 'yellow'); //example color

OR

CSS:

#inputbox{color: yellow;} //example color
Domain
  • 11,562
  • 3
  • 23
  • 44
  • Its not about changing text color, that I would not have been a problem. I actually want to highlight the text, as in selected text, exactly what tag offers. – Abhi Feb 17 '15 at 07:02
  • Then you should create a DIV look like text field as @Abovestand suggested. For retaining the value in the field, you can make it as hidden field. – Domain Feb 17 '15 at 07:26