0

I am trying to make sure that when I click an image

<div id="getImage" class="gallery_grid_item md-card-content">
<img onClick="handleImage()" src ="img.png">
</div>

<div id="holdImage">
</div>

then it is placed into another div

function handleImage() {
        var $img = $("#getImage").children("img");
        $("#holdImage").append($img);
    }

However, I've double and triple checked this but the function still shows as undefined in the console?

What's going on here?

10
  • 2
    You forgot the closing " in <img onClick="handleImage() src ="img.png"> Commented Mar 20, 2019 at 13:23
  • also "getImage" implies your targetting an element. You probably forgot to add the id or class specifier (#, or . ) Commented Mar 20, 2019 at 13:28
  • Also getImage should be prefix with # or .... Commented Mar 20, 2019 at 13:28
  • @Mamun I've added # but unfortunately the quote was just a typo on here. still no luck Commented Mar 20, 2019 at 13:44
  • Could add all the relevant HTML as part of the question.... Commented Mar 20, 2019 at 13:45

3 Answers 3

2

it was caused by something that you missed and I correct all of that

  1. $("getImage") is replaced by $("#getImage") ,(you missed one # here)
  2. onClick="handleImage() is replaced by onClick="handleImage()" ,(you missed one " here)

  3. var $img is replaced by var img (you don't need to have a $ sign to declare a variable in js.

function handleImage() {
            var img = $("#getImage").children("img");
            $("#holdImage").append(img);
        }
#getImage{
width:50px;
height:100px;
border:1px solid #000;
}
#getImage>img{
width:100%;
height:100%;
}

#holdImage{
width:100px;
height:200px;
border:1px solid green;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="getImage" class="gallery_grid_item md-card-content">
    <img onClick="handleImage()" src ="http://imgsv.imaging.nikon.com/lineup/lens/zoom/normalzoom/af-s_dx_18-140mmf_35-56g_ed_vr/img/sample/img_01.jpg" >
 </div>

    <div id="holdImage">
    </div>

Sign up to request clarification or add additional context in comments.

1 Comment

Would it be causing problems for me if that html is inline in a JS function like this: function appendSomeItems(url, id, name, style) { return '<div><div class="md-card md-card-hover"> <div id="getImage" class="gallery_grid_item md-card-content"> <img onClick="handleImage()" class ="uk-align-center"></a> <div class="gallery_grid_image_caption"> <span class="gallery_image_title uk-text-truncate">' + name + '</span> <span>' + style + '</span> </div></div></div></div>'; }
0

You didn't close the quotes on onClick="..." so it may be looking for a function named "handleImage() src="

2 Comments

Ok so that was actually a typo on here, but I do have quotes added in my code. Also I added a '#' to my getImage but still no luck
Why are you naming a variable '$img'? I would be wary of using the jQuery operator ($) in a variable name. Have you tried changing that variable name?
0

You forgot to close the src attribute <img onClick="handleImage()" src ="img.png">

Comments

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.