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

  • SEARCH
  • Home
  • 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 8906711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:43:32+00:00 2026-06-15T02:43:32+00:00

I am trying to change the HTML tag and remove the class/style attributes after

  • 0

I am trying to change the HTML tag and remove the class/style attributes after the tag.
I already know how to do this if I create the code before hand and replace, now I want to know how to find the tags on an already loaded page and replace them with my js.

var s = "<h2 class=\"title\" style=\"font-color: red;\">Blog Post</h2>";
s = s.replace("<h2 class=\"title\" style=\"font-color: red;\">","<p>");
s = s.replace(/<\/h2>/g, "</p>");

Start with
<h2 class="title" style="font-color: red;">Blog Post</h2>

End with
<p>Blog Post</p>

So the question is how can I create the var s with existing HTML?
How do I find h2.title on a page and give it to var s?

edit I have no javascript experience except for this script which I found and tweaked.
Please explain how I can grab text from an existing document, and make it my var for s.replace to manipulate.

  • 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-06-15T02:43:33+00:00Added an answer on June 15, 2026 at 2:43 am

    Don’t try to do it with regular expressions, you should use DOM manipulation to move the text node in question to a p tag you create. Here’s some code that will do what you need.

    http://jsfiddle.net/jWRh5/

    // Find the h2
    var h2 = document.querySelector("h2");
    // Create the p element you need
    var p = document.createElement("p");
    // Move the text node into the p element
    p.appendChild(h2.firstChild);
    // Insert the p element before the h2
    h2.parentNode.insertBefore(p, h2);
    // Get rid of the h2
    h2.parentNode.removeChild(h2);
    

    If you want to go against what every one else suggests, here’s a way to use RegExp to achieve what you need http://jsfiddle.net/jWRh5/1/

    It uses a not well supported feature, outerHTML (it does work in most recent versions of the major browsers)

    var h2 = document.querySelector("h2.title");
    var s = h2.outerHTML;
    s = s.replace("<h2 class=\"title\" style=\"font-color: red;\">","<p>");
    s = s.replace(/<\/h2>/g, "</p>");
    h2.outerHTML = s;
    

    Here’s how to do it for all h2.titles on your page (not using the RegExp way since that’s a really poor way, but if you’re really set on using it, you can use it as a guide)

    var h2s = document.querySelectorAll("h2.title");
    // Loop backwards since we're modifying the list
    for (var i = h2s.length -1 ; i >=0; i--) {
        var h2 = h2s[i];
        var p = document.createElement("p");
        p.appendChild(h2.firstChild);
        h2.parentNode.insertBefore(p, h2);
        h2.parentNode.removeChild(h2);
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to change a tag of a html element. HTML <div id='content'>
I am trying to change the checked attribute of dynamically created html radio button
I am trying to change the padding of the DIV tag that is containing
I am trying to remove <br /> tag from wordpress native gallery through jquery
Im trying to change the value to the src attr of the img tag
I am trying to change the HTML image src using Javascript. I have two
I'm trying to change the display CSS property of a <div> tag when the
I am trying to solve a problem to remove HTML tags from a string.
Possible Duplicate: Use jQuery to change an HTML tag? I have searched around for
I'm just trying to add some string or HTML tag inside div #OL_Icon_63 using

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.