X

LATEST #JQUERY NEWS

How to Use jQuery to Search and Replace HTML


Have you ever made a mistake in your code or in your text and dreaded combing through all of your HTML files to repair what went awry? With this jQuery search and replace code, you may never have to do that again. The code searches for a term or word in your HTML files and then replaces that term or word with whatever you like, much like the search and replace functionalities on your favorite word processor or text editor.

If you want to see how the code works, take a look at the snippet below:

$(function() {
    $(":contains(FIND)").not(":has(:contains(FIND))").each(function() {
        var that = $(this),
            html = that.html();
        html = html.replace(/(\(FIND:.*?\))/g, "REPLACE-WITH");
        that.html(html);
    });
});

If you want to use the example above to perform¬†your own search and replace functionality, you’ll need to replace the word “FIND” with the phrase you’re searching for, and the term “REPLACE-WITH” with, naturally, whatever you’d like to replace the “FIND” element with.

Make sure you exercise caution when using this snippet, because you could potentially cause some serious errors if you accidentally search and replace some valuable/necessary code or HTML elements. This code will replace every single instance of the “FIND” item, so always bear that in mind.

Recently Published

article image
»

7 Responsive jQuery Dropdown Menu Plugins

Dropdown Menu Plugins Dropdown menus are mostly used for website ...

article image
»

20 Stylish and Modern jQuery Tooltip Plugins

jQuery Tooltip Plugins Tooltips are a great option for showing extra ...

article image
»

15 Newest jQuery plugins for responsive website

Due to the proliferation of smartphones and tablets, responsive ...

article image
»

How to use Redux with jQuery

State management is one of most integral parts of any software and a ...

article image
»

Fade HTML Table Row on Hover Using jQuery

HTML tables are the ideal choice for displaying data in tabular form. ...

article image
»

Drag and Drop HTML Table Rows Using jQuery

HTML tables are the preferred UI option for displaying data. Sorting, ...

article image
»

Top 5 Best JavaScript Template Engines

Template engines help in the decoupling of HTML and JavaScript code ...

article image
»

3 jQuery/JavaScript Plugins for Push Notifications

Push notifications started from the mobile business but this ...

article image
»

Create Smooth Animation Using jQuery’s “stop()”

Making Quick Work jQuery’s animate() method is a quick way to ...