0

I have this text file filled with some html code;

<span>test</span><p>welcome</p>
<span>Guest</span><p><img class="insertedImage" src="/dynamictemplate/uploads/temp/167-5-IMG_0755.JPG" width="80" height="80"/></p>

I'm reading this html data into a div element, however when I try to add a click event to the 'img' tag it's not working. I know it reads the the file correctly because if I add a css targeting the '.insertedImage' it works fine, but when I try to add a click event its not doing anything.

$('.insertedImage').click(function() 
{
  alert("clicked");
});

Anyone has an idea about what causing this problem?

2 Answers 2

1

Assuming your content loading dynamically and then your event is not fired.

Then try this event on()

In your case

$(".insertedImage").on("click",  function(event){
    alert($(this).text());
});
Sign up to request clarification or add additional context in comments.

3 Comments

No it didn't work. in fact for some reason, it broke all my other jquery events.
@akk kur What jquery version are you using? If < 1.7, you can try using delegate() instead of on(), because if IIRC on() was added from 1.7 on
oh I'm using 1.6.3 I think that's why its not working. I'm gonna try with the 1.7
1

Try to wrap it around $(document).ready()

$(document).ready(function()
{
    $('.insertedImage').click(function() 
    {
       alert("clicked");
    });
});

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.