0

I want to change my img src with jQuery click function but can't seem to do it. Here is my html code:

<div id="main2"><img id='mty' style="width: 500px;height: 600px;"
src="https://cdn.pastemagazine.com/www/articles/morty%20main.jpg">
</div>

Here is my jQuery code that won't work:

<script type="text/javascript">
$('#paper').click(function() 
{
$('#mty').attr('src', 'http://www.cliparthut.com/clip-arts/1008/paper-stack-clip-art-1008442.jpg');
});  </script>

what am I doing wrong? I want to be able to change the html src if I click on #paper which is a button btw.

1
  • Where is element with id paper. Can you please add button tag also in question? Commented Sep 23, 2017 at 20:33

3 Answers 3

2
$('#mty').attr('src', 'http://www.cliparthut.com/clip-arts/1008/paper-stack-clip-art-1008442.jpg');

You need to set "src" to img tag, not the div tag

<script type="text/javascript"> 
    $(document).ready(function(){   
        $('#paper').click(function() { 
           $('#mty').attr('src', 'http://www.cliparthut.com/clip-arts/1008/paper-stack-clip-art-1008442.jpg'); 
       }); 
   }); 
</script>
Sign up to request clarification or add additional context in comments.

5 Comments

Add your click event within document.ready.. $(document).ready(function(){ //add click listener here });
try this script; <script type="text/javascript"> $(document).ready(function(){ $('#paper').click(function() { $('#mty').attr('src', 'cliparthut.com/clip-arts/1008/…); }); }); </script>
Where is element with id paper, which is used as click?
Such mistake happens in Javascript. Glad you made it.
one type took so long for me to find
0

Html is as-

<button type="button" id="paper">change image</button>
<div id="main2">
    <img id='mty' style="width: 500px;height: 600px;"
         src="https://cdn.pastemagazine.com/www/articles/morty%20main.jpg">
</div>

Script-

<script type="text/javascript">
    $('#paper').click(function () {
        $('#mty').attr('src', 'http://www.cliparthut.com/clip-arts/1008/paper-stack-clip-art-1008442.jpg');
    });
</script>

Things to remember-

User script after html render or use jquery ready.

Comments

0

<!DOCTYPE html>
<html>

  <head>
    <meta charset="utf-8" />
    <title></title>
    <link rel="stylesheet" href="style.css" />
    <script data-require="jquery" data-semver="3.1.1" src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
  </head>

  <body>
   <img id="image" src="http://lorempixel.com/400/200" alt="random image"/>
   <button id="changeImage">Change image</button>
   <script>
     $('#changeImage').on('click', function(){
    
    $('#image').attr('src', 'http://lorempixel.com/400/200');
     });
   </script>
  </body>

</html>

2 Comments

Could you add some explanation as to what the OP (original poster) did wrong. Other than that, nice answer. I like that you've added in the ability to run the code snippet.
Thanks, Original poster not post details code. That's why I put details.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.