tags:

views:

295

answers:

2

HTML:

<div class="someclass" rel="first">text 1</div>
<div class="someclass" rel="second">text 2</div></div></div>
<div class="info_ly">here is some text</div>
<div class="first" > the first DIV </div>
<div class="second" > the second DIV </div>

CSS:

.first{ display:none}
.second{ display:none}

Jquery:

$(".someclass").click(function() {  
$(".info_ly").html($(this).attr('rel'));
});

I want to call and load the "rel" DIV inside "info_ly" DIV.

With this Jquery code I get only text "first" or "second" inside "info_ly" DIV.

How can I load the DIV with the class "first" or DIV with the class "second" inside "info_ly" DIV?

A: 

Try this:

$(".someclass").click(function() {  
    $(".info_ly").html($(this).html());
});
Luca Matteis
+2  A: 

Like this:

$(".someclass").click(function() {  
  $(".info_ly").html($("." + $(this).attr('rel')).html());
});

You can view a demo here, if the other divs had IDs though, it gets a bit simpler, e.g. rel="#first", or making the clickable elements anchors with href="#first" would be even better.


Here's an example of the alternative approach I mentioned using anchors:

<a class="someclass" href="#first">text 1</a>
<a class="someclass" href="#second">text 2</a>
<div class="info_ly">here is some text</div>
<div id="first" class="contentDiv"> the first DIV </div>
<div id="second" class="contentDiv"> the second DIV </div>​

And your jQuery narrows down to this:

$(".someclass").click(function() {
  $(".info_ly").html($(this.hash).html());
});​

This has the added benefit of degrading gracefully when JS isn't enabled (as well as being bookmarkable if you add just a bit of code), you can see a demo of this approach here.

Nick Craver
Thanks Nick.The problem if I use this HTML code:<div class="someclass" rel="first">text 1</div><div class="someclass" rel="second">text 2</div><div class="someclass" rel="info_ly">text 3</div><div class="info_ly">here is some text</div><div class="first" > the first DIV </div><div class="second" > the second DIV </div>​I can't call the "info_ly" div with rel "info_ly". How can i do that?
Sergio
@Sergio - I don't follow...that would take the content in `info_ly` and just put it back there, having no effect, can you explain better what you're after?
Nick Craver
@Nick - This should work as a menu. If someone click on "text 3" DIV it should open the DIV that was there when the page was loaded. In this case, it did not open if it's clicked.You can try that code at:http://jsfiddle.net/DeFpY/
Sergio
@Sergio -It **did** load, but as I said you're loading the same content into the same thing, having no effect, see what I mean here: http://jsfiddle.net/TbTgr/
Nick Craver
@Nick - Yes, I see now. Sorry. The thing that I want to do is to load content "here is some text" again if the "text 3" was clicked. How can I do that? Put it in another DIV?
Sergio
@Sergio - Yes you'd want to use another div instead of `.info_ly` for the actual display, alternatively do you *need* to copy the content? Can you just toggle which of the `<div>` elements with content are visible?
Nick Craver
@Nick - I need to switch between these three divs using clicks on menu (text 1, text2 or text 3). Only the clicked div should be visible. If someone click on text 1 the "first" div should show up, if the text 3 is clicked - the "second" DIV will show up and if the text 3 is clicked the text "here is some text" must be visible. The same text "here is some text" should be visible at page load as well. I thought the easiest way to do it is using this code ... or is it wrong?
Sergio