jQuery

[jQuery] change image with jquery

OOQ 2022. 7. 22. 16:42
728x90
SMALL

How to change the image with the attr function.

<html>
	<img id="imgID" class="imgClass" src="/data/img.png"/>
</html>

<script>
	$(document).ready(function(){
    	//Image change function call
    	changeIMG();
    });
    
    function changeIMG(){
    	//When changing to the ID of the img tag
    	$("#imgID").attr("src", "/data/img2.png");
        
        //When changing to class name of img tag
        $(".imgClass").attr("src", "/data/img2.png");
    }

</script>

It doesn't matter if you change the id value or the class name.

$("#imgID).attr("src","/data/img2.png");

Just remember that phrase.

728x90
LIST