0

I have a click event inside of which I would like to load different divs according to what has been clicked:

var open = $('.open');
open.click(function(evt) {
    evt.preventDefault();
    overlay.fadeIn(400, function(){
        box.fadeIn(500);
        // if something load box2
        // if something load box3
    });

http://jsfiddle.net/bLDXS/

Open is a class given to the links which relate to box, box2 and box3. How can I load the content for box2, would I have to use a specific class for each of them?

6
  • 1
    I don't understand the question. Are you asking how to write an if condition? Commented Oct 25, 2012 at 10:21
  • 1
    You need to post the html that corresponds to your posted code so we can help you Commented Oct 25, 2012 at 10:22
  • I know how to write one, but how specifically in this case to load either box1, or box2 or box3? Commented Oct 25, 2012 at 10:22
  • Please add your HTML code to the question Commented Oct 25, 2012 at 10:23
  • yo, check your DOM class .open is not there in HTML! :) how do you trigger click event? Commented Oct 25, 2012 at 10:28

3 Answers 3

1

Edit: After looking at the fiddle, you'll have to give each clickable <p> an id, and load the corresponding box.

Html:

<p class="terms open" id="box1_clicker">I agree to the <a href="">terms and conditions.</a></p>

Js:

if($(this).attr("id")=="box1_clicker") {
    // do something
}

Example fiddle


You could specify an id for each of the boxes, and then check inside the click which box's link was clicked, using jquery's parent():

if($(this).parent().attr("id") == "box1") {
    //some code
}
else if ($(this).parent().attr("id") == "box2") {
    //some code
}
// so on
Sign up to request clarification or add additional context in comments.

Comments

1

Open is a class given to the links which relate to box, box2 and box3. How can I load the >content for box2, would I have to use a specific class for each of them?

No, you can simply write your code with if statement:

var open = $('.open');
open.click(function(evt) {
   evt.preventDefault();
    overlay.fadeIn(400, function(){
    box.fadeIn(500);
    if (cond1){ load box2 }
    else if (cond2) {load box3 }
});

2 Comments

would the condition 1 be another click event saying if you click on .box2(for example) do this.....
No the condition cannot be another click event. I think your question so not clear enough. From what it seems you want to distinguish between which div / box generated the even and based on that condition you want to show some UI manipulations. If that is that case then you can check for id with $("this").attr('id') if using jquery.
1

Try this:

var open = $('.open');
open.click(function(evt) {
    evt.preventDefault();
    box= $(this).attr('rel');//get the rel of your clicked link.
    overlay.fadeIn(400, function(){
        $('#'+box).fadeIn(500);//fadein the div your link relates to if it is your div's id.

    });

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.