2

I want to write a function that surrounds an image with a border frame each time i click on it. unf. i can't make it work. deny any typos cause i didn't copy paste it. the function works. but no affect is being made on the image when clicking on it. did i access the border attribute right?

my function:

<script>

function mark(imageId) {
    document.getElementById(imageId).style.border = "1";
}

</script>

my html body:

<input id="imageId" src="\images\image1.png" onclick="mark(imageId)"/>

6 Answers 6

9

Your markup doesn't quite make sense, but:

<input id="imageId" type="image" src="https://www.gravatar.com/avatar/e1122386990776c6c39a08e9f5fe5648?s=128&d=identicon&r=PG" onclick="mark(this)"/>

function mark(el) {
    el.style.border = "1px solid blue";
}

http://jsfiddle.net/8QGkq/

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

2 Comments

Great works! and what is the right attribute for cancelling this border to the default no border style?
@JaredFarrish , After select and unselect there is still a blue shadow there , can you please tell how to remove it ? and yes +1 for the answer :)
2

Setting the border to "1" didn't work for me. Try this:

<script>

function mark(imageId) {
    document.getElementById(imageId).style.border = "1px solid black";
}

</script>

You'll also need to surround imageId in the HTML in quotes (not sure if that was a typo or not):

<input id="imageId" src="\images\image1.png" onclick="mark('imageId')"/>

2 Comments

that's good! now it's working! w3schools said style.border="1" should be enough - i guess not. and if i want to cancel the border should i change it to empty quotes " " ?
@UdiLivne - See my comment above. I forgot to notify you with the @username.
2

You don't want getParameter(). You want getElementById().

You also don't want the variable name imageId surrounded by quotes inside the function declaration for mark() because that changes it to a string.

And as @John Girata points out, you want to specify more than just "1" for the border value.

document.getElementById(imageId).style.border = "1px solid black";

Furthermore, you need to quote "imageId" in your onclick attribute:

<input id="imageId" src="\images\image1.png" onclick="mark('imageId')"/>

2 Comments

sorry my bad i did use elementById - still doesn't work.. fixed also the quotes:) still doesn't work
You also don't want imageId in quotes as a parameter for getElementById().
2

This is a code for that question.

<html>
<head>
<title></title>
</head>
<body>
<script type="text/javascript">
    function sayHello() {
        var boyElement = document.getElementById("boy");
        boyElement.style.border = "3px solid red";
    }
</script>
<img src="C:\Users\Acer\Desktop\new web site\js\images\boy.png" id="boy" 
onclick="sayHello()">
</body>
</html>

Comments

0

To clarify, setting borders with a DOM element, you need to provide width, color, style like:

document.getElementById("ex1").style.border="1px solid #0000FF";

w3Schools actually says: http://www.w3schools.com/jsref/prop_style_border.asp

2 Comments

@Jared Farrish - I wasn't really, just pointing out that they actually did give the right answer!
We're all lucky part of the time. Try MDN, on SO you won't get harassed.
0

Look at this :

<img id="CN" src="CN.png" onclick="fnChangeBorder('CN')">

and define your function:

function fnChangeBorder(imageId)

{document.getElementById(imageId).style.border = "solid #AA00FF";}

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.