0

Please can somebody tell me how to do the following

.logo {
display: block;
text-indent: -9999px;
text-transform: capitalize;
background: url(../images/menu.png) no-repeat;
height: 120px;
}

change image so when its clicked

background: url(../images/menu_open.png) no-repeat;

the HTML for this is

<div class="sidebar">
<a href="#" onclick="return showOrHide('menulink');"><div class="logo">TEST IMAGE</div></a>
<ul id="menulink">

Currently when clicking the icon, the menu opens and closes,

view the full codes at http://carbonyzed.co.uk/menu/2/index.html http://carbonyzed.co.uk/menu/2/css/style.css

Thanks

4
  • Do you want the background image to change permanently (until refresh or reload), or just for as long as the mouse is clicking it? What's a test image in this context? In what way is it different from a regular one? Commented Nov 6, 2012 at 13:40
  • idealy, to toggle with menu, please see carbonyzed.co.uk/menu/2 Commented Nov 6, 2012 at 13:42
  • Then I suggest that you follow @Lokase's advice, or use jQuery's show feature. Commented Nov 6, 2012 at 13:43
  • please see comment on @Lokase's answer, thank you :) Commented Nov 6, 2012 at 13:44

3 Answers 3

3
$(".logo").click(function() {
  $(this).css('background-image', 'url(../images/menu_open.png)');
});
Sign up to request clarification or add additional context in comments.

6 Comments

Really? Where has he mentioned jQuery solution? and the jquery you have written is wrong - jsfiddle.net/LdErY
You can't operate the click handler with pure html / css
Jquery solution would work, however I currently have an OnClick in there for toggling the menu, can you apply multiple OnClick results? and also, can I apply an OnClick in this case, where the image is located in the CSS? Thanks
Dipak, the dude is already using jquery, so there's no need to jump all over Lokase, and you look silly now.
@puppybeard you should be careful with the comments you are posting, it can be flagged... and the solution Lokase has posted is wrong it doesn't work. it has to be $(this).css('background-image', 'url(../images/menu_open.png)');
|
1

For the best user experience, consider using a sprite, and changing the class on click, rather than changing the style attribute. So imagine you have the two images in one sprite, called menu_sprite.png

The image is 240px high, and the width of the logo. The default image is in top left, the "open" image is right underneath.

CSS

.logo{
  background-image: url(../images/menu_sprite.png) 0 0 no-repeat;
}

.logo.open{
  background-position: 0 -120px;
}

jQuery

$('.logo').click(function(){ $(this).toggleClass('open'); });

This method lets you avoid having a split-second with no image, without using preloaders.

1 Comment

Glad you got it working. For an example where things aren't resizing it's very handy.
0

This should work

<a href="#" onclick="return ShowOrHide('menulink'); document.getElementById('id').style.background = 'url(img.img) no-repeat';" />;

5 Comments

I currently have '<a href="#" onclick="return showOrHide('menulink');"><div class="logo">TEST IMAGE</div></a>' so can i make it '<a href="#" onclick="return showOrHide('menulink');""document.getElementById('id').style.background = 'url(img.img)' no-repeat" />;' ???
so - <a href="#" onclick="return showOrHide('menulink');";"document.getElementById('id').style.background = 'url(img.img)' no-repeat" />;
<a href="#" onclick="return ShowOrHide('menulink'); document.getElementById('id').style.background = 'url(img.img) no-repeat';" />;
in fact - <a href="#" onclick="return showOrHide('menulink')";"document.getElementById('id').style.background = 'url(img.img)' no-repeat" />
No.. dont close the quotes and open them again... "return showOrHide('menulink')";"document.getElementById('id').style.background = 'url(img.img)' no-repeat" should be "return showOrHide('menulink'); document.getElementById('id').style.background = 'url(img.img)' no-repeat"

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.