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 7657939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:07:58+00:00 2026-05-31T13:07:58+00:00

What I am trying to do : As stated in the title, I want

  • 0

What I am trying to do: As stated in the title, I want to set the CSS of a word if it is a reserved word.


HTML

<html>
    <body>
        <code id="java">
            public static void main(String[] args)<br>
            {
                <pre>    System.out.println("Hello World!");</pre>
            }
        </code>
    </body>
</html>

jQuery

$(document).ready(function()
{
    // Get the text inside the code tags
    var code  = $("#java").text();

    // Split up each word
    var split = code.split(' ');

    // Array of reserved words
    var array = ["abstract","assert","boolean","break","byte","case",
                 "catch","char","class","const","continue","default",
                 "do","double","else","else if","enum","extends","final",
                 "finally","float","for","goto","if","import","implements",
                 "instanceof","int","interface","long","native","new","null",
                 "package","private","protected","public","return","short",
                 "static","strictfp","super","switch","synchronized","this",
                 "throw","throws","transient","void","volatile","while"];

    // Added when text contains a reserved word
    var css = {'font-weight':'bold','color':'#2400D9'}

    array = jQuery.map(array, function(n,i)
    {
        for (int j=0; j<array.length; j++)
        {
            if (split[i].contains(array[j]))
                split[i].css(css);
        }
    });
});

Problem: I have referred to the documentation for several methods (in the references section below), but I’m not too sure where the problem(s) lies. To narrow the issue down, my question(s) would be…

  1. Is .split() even a method in jQuery?
  2. Should I use a for loop to run through all the words in the array (to see if the code contains a reserved word) or is there a better approach (such as .each())?
  3. If I should use .each(), could someone please give me a simple example? I don’t understand the examples in the documentation.

References

  • .split() in jQuery
  • .map()
  • .each()
  • 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-31T13:08:00+00:00Added an answer on May 31, 2026 at 1:08 pm

    If I understood correctly, you can achieve what you want using $.inArray and wrapping the reserved word with a span tag. See my DEMO

    Edit: Below is from jQuery $.inArray documentation.

    $.inArray( value, array [, fromIndex] ) –

    valueThe value to search for.

    arrayAn array through which to search.

    fromIndexThe index of the array at which to begin the search. The
    default is 0, which will search the whole array.

    ..read more..

    CSS

    .code {
        font-weight: bold;
        color: #2400D9;
    }
    

    JS

    $(document).ready(function() {
        // Get the text inside the code tags
        var code = $("#java").html();
    
        // Split up each word
        var split = code.split(' ');
    
        // Array of reserved words
        var array = ["abstract", "assert", "boolean", "break", "byte", "case", "catch", "char", "class", "const", "continue", "default", "do", "double", "else", "else if", "enum", "extends", "final", "finally", "float", "for", "goto", "if", "import", "implements", "instanceof", "int", "interface", "long", "native", "new", "null", "package", "private", "protected", "public", "return", "short", "static", "strictfp", "super", "switch", "synchronized", "this", "throw", "throws", "transient", "void", "volatile", "while"];
    
        for (var j = 0; j < split.length; j++) {
            if ($.inArray(split[j], array) > 0) {
                split[j] = '<span class="code">' + split[j] + '</span>';
            }
        }
    
        $("#java").html(split.join(' '));
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

As the title states, I'm trying to resize a Texture2D before even considering SpriteBatch.Draw()
I started trying to set up a local rails installation for development. After I
I started by trying to add a .toggle() to this code so that the
I'm having trouble trying to map my EF 4.1 Code First model to a
Trying to understand this binding process of the WPF. See the code at the
As title states, my objective is to produce an HTML table using data from
Just a simple question, just as the title states. I just want to know
I have been trying to learn about resources and styles, I want to create
Recently started programming Android Java (Eclipse), Im trying to make a simple reader app
As the title states, I am trying to import an UIView, but I can't

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.