Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6612205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:04:08+00:00 2026-05-25T20:04:08+00:00

I have a function $(document).ready(function () { $(#btnhighlight).click(function () { var htext = $(#txthighlighttext).val();

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T20:04:08+00:00Added an answer on May 25, 2026 at 8:04 pm

    I didn’t understand where does l is in your current code. Anyway, you can create a DOM element with jQuery like this: var myel = $('<div class="someclass"></div>'). Then you can append myel to 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):

    $(document).ready(function () {
        $("#btnhighlight").click(function () {
            var htext = $("#txthighlighttext").val();
            $("#lstCodelist option").each(function () {
                var sp = $(this).text();
                    var re = new RegExp("\\b" + htext + "\\b")
                    sp = sp.replace(re, '<span class="highlighted">$1</span>', "gi");
                    $(this).html(sp);
            });
        });
    });
    

    You also need some CSS on your page, defining the style for .highlighted

    UPDATE 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):

    $(document).ready(function () {
        $("#btnhighlight").click(function () {
            var htext = $("#txthighlighttext").val();
            //$("#lstCodelist option").each(function () {
            $("p").each(function () {
                var sp = $(this).text();
                var re = new RegExp("\\b(" + htext + ")\\b")
                var sOpen = "<span class='highlight'>";
                var sClose = "</span>";
                var newhtml = sp.replace(re, sOpen + "$1" + sClose, "gi");
                $(this).html(newhtml);
            });
        });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

$(document).ready(function () { $(#btnhighlight).click(function () { var htext = $(#txthighlighttext).val(); if (htext == '')
I have this JQuery function: $(document).ready(function() { $(div.descript_long).each(function() { var descript_length = $(div.descript_long).text().length; if(descript_length
I have this function: $(document).ready(function(){ function Fos(buttonSel, inputSel, someValue, cssProperty) { $(buttonSel).click(function(){ var value
Really quick jQuery question... I have this function: $(document).ready(function() { $('a#show1').click(function() { $('.item1').toggle(1000); return
i have a code like this <script>$(document).ready(function(){ var myPlayList = []; //document.write(whatTo); //New instance
I have this code: $(document).ready(function() { var number = 10; var offset = 10;
I have this javascript function: jQuery(document).ready(function() { FB.api('/me/friends', function(response) { if(response.data) { $.each(response.data,function(index,friend) {
i think that i have a sintax error in this code <script type=text/javascript> $(document).ready(function()
Under $(document).ready(function() , I have the following code: num = $(#mainContentCategoryLinksWrapper > div).size(); This
I have this jQuery: $(document).ready(function() { $(#panel).hide(); $('.login').toggle( function() { $('#panel').animate({ height: 150, padding:20px

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.