Search code examples
htmlcsskeywordmarkup

How can I change style of a defined keyword without explicit markup.


In a website, is there any way of changing text attribute of keyword, like product name, in an entire website without using any kind of html tags wrapped around those instances? I may require to define those keywords in server side environment or there may be a server side script in which I may need to define. Or can we use client side Javascript like some jquery plugin?


Solution

  • You can use Javascript on the client side to achieve this.
    Drawing from this answer the following is a simple example:

    Sample Jsfiddle

    JS:

    $('*').contents().each(function(){
        if(this.nodeType === 3) {
            $(this).replaceWith(this.wholeText.replace(/target/g, 'replaced'));    
        }
    });
    

    HTML:

    <h1>target</h1>
    <p>Here it is: <span>target</span> and target</p>