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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:20:02+00:00 2026-05-25T18:20:02+00:00

I have some JQuery code that converts all HTML elements of a specific class

  • 0

I have some JQuery code that converts all HTML elements of a specific class to & from textarea elements.

My Problem: I use JQuery(.addClass()) to change an elements class from “updatable” to “updatable P”. But when I go to search for all the elements that have the class “updatable” (using the function $(“.updatable”).each()) it does’nt find any elements when it should find 3.

What am I doing wrong to make this happen? It seems that after I change an elements classI am unable to identify/find that element by its class(using JQuery) again.

<html>
<head>

    <script type="text/javascript" src="jquery-1.6.4.js"></script>
    <script type="text/javascript">
    <!--
        var STATE = 1;

        function Toggle()
        {
            if (STATE==1) { convertToUpdatable(); STATE = 0; }
            else          { convertToStatic();    STATE = 1; }
        }

        function getTitleName( ele )
        {
            try        { return ele.className.split(" ")[1]; }
            catch (ex) { return "undefined"; }
        }

        function convertToUpdatable()
        {
            // Post: Convert all HTML elements (with the class 'updatable') to textarea HTML elements
            //       and store their HTML element type in the class attribute
            // EG: Before: <p class="updatable Paragraph1"/> Hello this is some text 1 </p>
            //     After : <p class='updatableElementTitle'>Paragraph1</p><textarea class="updatable Paragraph1 p"/> Hello this is some text 1 </textarea>

            $(".updatable").each(function()
                {
                    var title = getTitleName( this );
                    $(this).replaceWith("<p class='updatableElementTitle'>"+title+"</p><textarea>"+$(this).text() +"</textarea>");
                    $(this).addClass( this.nodeName );
                    alert( this.className );
                });
        }

        function convertToStatic()
        {
            // Post: Find all HTML elements (with the class 'updatable'), check to see if they are part of another class aswell
            //       (which will be their original HTML element type) & convert the element back to that original HTML element type

            // PROBLEM OCCURS HERE: after I have changed an elements className in the convertToUpdatable() I can no
            //                      longer find any HTML elements that have the className updatable using $(".updatable").each()
            $(".updatable").each(function()
                {
                    alert("Loop");
                    // Determine elements original HTML(node) type
                    try
                    {
                        var type = this.className.split(" ");
                        type     = (type[ type.length-1 ]).toLowerCase();
                        alert(type);
                    }
                    catch (ex) { alert("Updatable element had no type defined in class attribute"); return; }

                    // Convert element back to original HTML type
                    $(this).replaceWith( type +$(this).text() + type );

                    // Remove elements type from its className (remove " P" from "updatable Paragraph1 P")
                    $(this).removeClass( type ); 
                    alert( this.className );
                });

            // Delete all elements with the class 'updatableElementTitle'
            $(".updatableElementTitle").remove();
        }

    -->
    </script>
</head>
<body>

    <p class="updatable Paragraph1"/> Hello this is some text 1 </p>
    <b class="updatable Paragraph2"/> Hello this is some text 2 </b>
    <i class="updatable Paragraph3"/> Hello this is some text 3 </i>

    <input id="MyButton" type="button" value="Click me!" onclick="Toggle();" />

</body>
</html>
  • 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-25T18:20:03+00:00Added an answer on May 25, 2026 at 6:20 pm

    .replaceWith() destroys the existing element. Thus, you can no longer use this after you’ve done the replaceWith() in the loop because that DOM element is no longer the element that’s in your document (it’s the old one that has been removed and will be garbage collected as soon as your function exits).

    Since you’re specifying the HTML for the new tag, I would suggest you just put the new class name in the HTML you pass to replaceWith(). Further, the alert you have to check the className is checking the old DOM element, not the new DOM element. Remember, this points to the old DOM name, not the one you replaced it with.

    You could do so by changing this:

            $(".updatable").each(function()
                {
                    var title = getTitleName( this );
                    $(this).replaceWith("<p class='updatableElementTitle'>"+title+"</p><textarea>"+$(this).text() +"</textarea>");
                    $(this).addClass( this.nodeName );
                    alert( this.className );
                });
        }
    

    to this:

            $(".updatable").each(function()
                {
                    var title = getTitleName( this );
                    $(this).replaceWith("<p class='updatableElementTitle " + this.nodeName + "'>" + title + "</p><textarea>"+$(this).text() +"</textarea>");
                });
        }
    

    Although, I don’t understand why you’re adding the nodeName as a class?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some jQuery code that finds elements with the shaded CSS class and
I have some jQuery code that copies the selected values from one listbox to
I have some jquery code that is doing an ajax lookup and returning comma
I have some jQuery code that highlights a link when clicked, and changes the
I have some jQuery code that intercepts links clicked on a page: $(document).ready(function() {
I have some jQuery/JavaScript code that I want to run only when there is
I have some jQuery code. I have called an Ajax function file, file.php, that
Trying to get comfortable with jQuery and I have encountered some sample code that
I have the following code in JavaScript and jQuery: $(<li />) .html('Some HTML') I
I have some jQuery code that runs fine until I add the jQuery UI

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.