views:

56

answers:

5
div.img
{   
    margin:7px;
    border:3px solid gray;
    height:110px;
    width:110px;
    float:left;
    text-align:center;
}   


div.img img 
{   
    display:inline;
    margin:3px;
    border:1px solid white;
}   


div.img a:hover img 
{   
    border:3px solid yellow;
}   


function handleMouseClick(imageName)
{
    document.getElementById(imageName).style.borderWidth = '3';
    document.getElementById(imageName).style.borderStyle = 'solid';
    document.getElementById(imageName).style.borderColor = 'yellow';
}

I have some JS and CSS code pasted above which renders borders images with borders and changes the image border on mouseclick to '3px solid yellow'. This code works as expected but I would like to know if there is a better way to do this through CSS instead of the JS code I have in handleMouseClick(...).

Secondly, when I select my image, the image border changes as expected to '3px solid yellow' but since the img element is a child of "<a href...> </a>", I also see a dotted blue border around the image. How do I get rid of the blue border?

A: 

You could alter your div.img a:hover img to apply to an additional case

div.img a:hover img, div.img img.selected
{   
    border:3px solid yellow;
} 

and then just add the selected with javascript upon click..

function handleMouseClick(imageName)
{
    document.getElementById(imageName).className  = 'selected';
}
Gaby
Thanks Gaby. This was helpful :-)
A: 

The cleanest way for js to manipulate html styles is by setting the class attribute. In your case have a class defined in CSS to have dotted border and set it using js when mouse click is detected. I quite didn't get the second question

questzen
A: 

Hi there,

A few issues here:

  1. Is your <A> tag really an anchor? Does it lead to something? If not, then you probably do not want/need to wrap your <IMG> in an anchor.
  2. As hinted in a previous answer, you would probably want to use CSS classes.

An example merging these suggestions is here:

<style type="text/css">
  img.myimage {
    border: 3px solid blue; /* assuming you want the blue border by default */
    cursor: pointer; /* Hint the user that the image may be clicked */
  }
  img.myimage-clicked {
    border: 3px solid yellow;
  }
</style>
<script type="text/javascript">
  function handleMouseClick() {
    document.getElementById("whatever").className = "myimage-clicked";
  }
</script>
...
<img id="whatever" src="" class="myimage" onclick="handleMouseClick()">
Sorin Mocanu
Hi Sorin,My image is not a real anchor. It does not lead to anything. But by having it as an anchor, I am able to set the border when I hover over the image through CSS. I know I can do the same thing through JS on mouseover event. Do you know of a better way to do this without the anchor tag? Appreciate your response. -k
Hi,I tend to favor hooking onmouseover and onmouseout and changing classes rather than using an un-semantic tag. You are right, you can depend on the :hover selector for the anchor (and not for the image, if you want to keep this cross-browser-compatible). But on the other hand if you are using a framework such as jQuery or the Google Closure Library adding event listeners comes quite cheap too.HthSorin
Sorin Mocanu
A: 

eh, people still use the DOM?

Use jquery instead:

//CSS:
.selected {
 border:3px solid yellow;
}

//Java Script
$("#myImgId").click(function(){
  $(this).addClass("selected");
});
mythz
Thanks Mythz. I did consider using jquery but I have skipped it for the time being. I will certainly be considering it in the immediate future.
A: 

The CSS value is outline. Use

a:active {outline: none;}

to remove the outline.

Residuum