0

This is my code in HTML:

<img id="test" src="https://blah">

This is in my jQuery and it works:

$( document ).on( 'mouseover', 'img', function(e) {
}

How can I add attribute to this 'img' element? I want this:

<img id="test" src="https://blah" title='test'>

I tried:

e.attr('title', 'test');
e.prop('title', 'test');
this.attr('title', 'test');
$(this).attr('title', 'test');
this.prop...

so on and so on.

1 Answer 1

1

You can use the hover() method:

$('img').hover(function() {
    $(this).attr('title', 'test');
});

In this case, $(this) represents the image that has been hovered over.

You can also do:

$('img').hover(function(e) {
    $(e.currentTarget).attr('title', 'foo');
});

In this case, $(e.currentTarget) represents the image that has been hovered over.

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

2 Comments

I would, but I need that e. I am using it to grab data about this img.
e.target.setAttribute('title', 'test'); helped me, which I took from your answer. But I left the $( document ).on( 'mouseover', 'img', function(e) {

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.