2

I need some JQuery code so that I can add a CSS border to an image when I click on it.

<img src="myimage" onclick="AddBorder();" />
0

3 Answers 3

17

If you're using jQuery, I'd recommend removing the inline onclick handler, as that's the less desirable way to bind to the event:

<img src="http://jsfiddle.net/img/logo.png" />

And then just setup a simple event binder:

$(function () {
  $("img").click(function() {
    $(this).css('border', "solid 2px red");  
  });
});

Here's a jsfiddle

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

Comments

1

In that onClick, instead of AddBorder();, place:

$(this).css("border","1px solid black");

Comments

-1

Er... what have you tried so far?

function AddBorder() { $(this).css('border','1px solid red'); }

1 Comment

Please don't mark -1 unless you also add a comment explaining why

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.