2

I'm trying to make a menu that's using css and jquery but i'm having problems for days now trying to figure out how to target the specific element.

here's my demo

CSS

    .contain {
    width:150px;
    height:150px;
    background-color:blue;
}
.contain:hover #arrow {
    cursor:pointer;
    display:block;
}
.show {
    display:block;
    background-color:green;
}
.hide {
    display:none;
}

JS

$("#arrow").click(function () {
    $("div > ul").css("display", "block");

});


$(".contain").mouseleave(function () {
    $(".contain > ul").css("display", "none");
});

Try to hover on that container, then click the arrow . the thing is they both show at the same time and i just can't figure my way out.. im new to jquery and i know this can be done.. please help.. btw, im not yet sure if these work on ie 8 and above but i have also to take these in mind... thanks in advance.

UPDATE: I guess i asked somewhat the wrong question, cause i needed to code it without using ID, cause each "profile" will be unique and be controlled by the backend guys and my only choice was to control CSS using jquery (as far as my knowledge goes). I did tried using pure CSS, but i am having problems with ":active" in IE =( so i look up on jquery and hopefully all is set. thanks all!!!

1
  • remove justin bieber, and everything will work. (sorry I had to say that) Commented May 31, 2013 at 9:15

4 Answers 4

7

Here's the FIDDLE

in jquery:

$("div.arrow").click(function () {
    $(this).next('ul').css("display", "block");

});


$(".contain").mouseleave(function () {
    $(".contain > ul").css("display", "none");
});

in html:

update <div id="arrow" to <div class="arrow"

Note: You can use id only once instead use class if you want to use it on several elements. Ok? :)

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

1 Comment

thanks!! i think this is the one im looking for! been working on this since the last 3-4 days lol. btw, im new here and is there a way to add reputation like i see in other post here?
1

The problem is that bot your arrow div and ul div have the same ID. ID must be unique in the page. try changing the div's id making 'em unique, like this:

http://jsfiddle.net/jjy6y/4/

and then update your jquery selector to achieve your results, like this:

$("#arrow1").click(function () {
    $("#dropdown1").css("display", "block");
});
$("#arrow2").click(function () {
    $("#dropdown2").css("display", "block");
});

Comments

1

Use $(this):

$("#arrow").click(function () {
    $(this).parent().find('ul').css("display", "block");
});

JSFiddle

Note: don't use the same ID for multiple elements. Use classes instead.

Comments

0

In your code you are binding the component with id "arrow" to the click event, just make different id like "arrow1" and "arrow2" for each div and link the click event

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.