homepage Welcome to WebmasterWorld Guest from 54.197.111.87
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
Toggle new div with jQuery on sibling
greencode




msg:4583766
 8:43 am on Jun 13, 2013 (gmt 0)

I have the following code…

<ul class="ppl clearfix">
<li class="bl">
<div class="image">
<div class="amt">22</div>
<img src="images/ph-image.jpg" alt="" />
</div>
</li>
<li class="bl">
<div class="image">
<div class="amt">4</div>
<img src="images/ph-image.jpg" alt="" />
</div>
</li>
</ul>

And then I'm trying to add a new div on click of the ".bl" div. What I have below works well but adds and removes the div to both of the li's I have above. I'm presuming I need to do something like sibling or closest but can't figure out where this needs to go in the code.

$(function(){
var NewContent='<div class="selected"></div>'
$(".bl").one('click', function(){
var $content = $(NewContent).insertAfter('.amt');
$(this).click(function(){
$content.toggle();
});
});
});

Any help or ideas would be greatly appreciated.

 

Fotiman




msg:4583844
 1:26 pm on Jun 13, 2013 (gmt 0)


$(function () {
var NewContent = '<div class="selected"></div>'
$(".bl").one('click', function () {
var $this = $(this),
$content = $(NewContent);
$this.find('.amt').after($content);
$this.click(function () {
$content.toggle();
});
});
});

greencode




msg:4583846
 1:58 pm on Jun 13, 2013 (gmt 0)

You are a genius! Thanks so much for this, really appreciated

Fotiman




msg:4583852
 2:08 pm on Jun 13, 2013 (gmt 0)

No problem. :) Glad to help.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved