I have a function
$(document).ready(function () {
$("#btnhighlight").click(function () {
var htext = $("#txthighlighttext").val();
$("#lstCodelist option").each(function () {
var sp = $(this).text();
var sp1 = sp.split(' ');
$.each(sp1, function (i, l) {
if (l == htext) {
var boldText = "<div style=\"background-color: yellow; display: inline; font-weight: bold;\">" + htext + "</div>";
$(document).append(boldText);
}
});
});
});
});
I updated the code but no luck.
Here in this code I need to create a l has a DOM element to apply color yellow to that.
please can any body help me out how to create a dom element.
Thanks
I didn’t understand where does
lis in your current code. Anyway, you can create a DOM element with jQuery like this:var myel = $('<div class="someclass"></div>'). Then you can appendmyelto wherever you want in the DOM using function like.appendTo(),.after(), etc.EDIT
You seem to be trying to highlight some words inside an
<option>element. I have doubts if that is going to work on all browsers, since form elements are a little tricky when it comes to CSS. You can try something like this (untested):You also need some CSS on your page, defining the style for
.highlightedUPDATE AFTER CHAT
I got something working using a
<p>instead of<option>, to rule out the problem of form element styling:http://jsfiddle.net/GTydh/3/
The updated js (in case fiddle is deleted):