[jquery] JQuery Find #ID, RemoveClass and AddClass

I have the following HTML

<div id="testID" class="test1">
        <img id="testID2" class="test2" alt="" src="some-image.gif" />
    </div>

I basically want to get to #testID2 and replace .test2 class with .test3 class ?

I tried

jQuery('#testID2').find('.test2').replaceWith('.test3');

But this doesn't appear to work ?

Any ideas ?

This question is related to jquery find addclass removeclass

The answer is


$('#testID2').addClass('test3').removeClass('test2');

jQuery addClass API reference


.....

$("#testID #testID2").removeClass("test2").addClass("test3");

Because you have assigned an id to img too, you can simply do this too:

$("#testID2").removeClass("test2").addClass("test3");

And finally, you can do this too:

$("#testID img").removeClass("test2").addClass("test3");

corrected Code:

jQuery('#testID2').addClass('test3').removeClass('test2');

Try this

$('#testID').addClass('nameOfClass');

or

$('#testID').removeClass('nameOfClass');

Examples related to jquery

How to make a variable accessible outside a function? Jquery assiging class to th in a table Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Getting all files in directory with ajax Bootstrap 4 multiselect dropdown Cross-Origin Read Blocking (CORB) bootstrap 4 file input doesn't show the file name Jquery AJAX: No 'Access-Control-Allow-Origin' header is present on the requested resource how to remove json object key and value.?

Examples related to find

Find a file by name in Visual Studio Code Explaining the 'find -mtime' command find files by extension, *.html under a folder in nodejs MongoDB Show all contents from all collections How can I find a file/directory that could be anywhere on linux command line? Get all files modified in last 30 days in a directory FileNotFoundError: [Errno 2] No such file or directory Linux find and grep command together find . -type f -exec chmod 644 {} ; Find all stored procedures that reference a specific column in some table

Examples related to addclass

Jquery assiging class to th in a table addClass and removeClass in jQuery - not removing class JQuery addclass to selected div, remove class if another div is selected How to: Add/Remove Class on mouseOver/mouseOut - JQuery .hover? How do I add a new class to an element dynamically? If hasClass then addClass to parent JQuery Find #ID, RemoveClass and AddClass

Examples related to removeclass

addClass and removeClass in jQuery - not removing class JQuery addclass to selected div, remove class if another div is selected How to: Add/Remove Class on mouseOver/mouseOut - JQuery .hover? Removing elements by class name? JQuery Find #ID, RemoveClass and AddClass