I’m implementing this: http://jsfiddle.net/xkfqN/85/
html:
<div id="posts">
<div class="more">Show More</div>
<div class="commentsContainer">
<div class="comment">Comment 1</div>
<div class="comment">Comment 2</div>
<div class="comment">Comment 3</div>
<div class="comment">Comment 4</div>
<div class="comment">Comment 5</div>
<div class="comment">Comment 6</div>
<div class="comment">Comment 7</div>
<div class="comment">Comment 8</div>
<div class="comment">Comment 9</div>
<div class="comment">Comment 10</div>
</div>
</div>
css:
.comment {
display: none;
border:3px solid whiteSmoke;
width:280px;
}
.more {
cursor:pointer
}
JQuery:
function toggleComment(index, hide) {
//check the index to make sure it is in range
if (index > -1 && $('.comment').length > index) {
//get the comment at the given index and apply the animation
$('.comment:eq(' + index + ')')
.slideToggle(18, function() {
//if hiding then decrement
var next = hide ? index + 1 : index - 1;
//call toggle on the next index
toggleComment(next , hide);
});
//set to display block so they show up on their own line
// .css('display', 'block');
}
}
$('.more').click(function() {
//are the comments already open... returns true or false
var hide = $(this).hasClass('open');
//default to start at the begining... each click brings the index to 0
var index = 0;
//if the comments are not open then start at the end
if (!hide) {
index = $('.comment').length - 1
}
//toggle the comments
toggleComment(index, hide);
//used to remember state.. adds class to more and takes it away hence toggle class
$(this).toggleClass('open');
});
The comments I intend to drop down aren’t shown by default and hidden with css like the example link shows as it wouldn’t be wise to load all comments by default for every user.
They’ll only be loaded on click of a “show all” link/submit. Anyway in the code below, my comments are grabbed from a file “show_all_comments” after it has been rendered on a page. In that file is a loop that loops through each comment and displays the data along with it’s html.
What I want to do:
I don’t want to render this on the page but some how in memory then be able to access the classes as if I was trying to access them from an actual page. This way I’ll be able to access each comments class and use with the JSFiddle code.
If that makes sense to you is this possible? If so how would I achieve this?
JQuery:
var postContent = $(this).parents('.post_content'),
commentContainer = postContent.find('.comment_container');
commentContainer.slice(0, -2).remove();
postContent.find('.comment_container:first')
.before("<%= j render 'users/partials/show_all_comments', :comments => @comments %>");
It sounds like you want to have disconnected DOM elements and search through them. You can do that quite easily:
There, I’ve created a structure with a single top-level
divwhich contains all of the others, and wrapped a jQuery instance around it. The elements are not in the DOM, they’re completely disconnected from it. Then I can use jQuery methods likefindand such to extract the bits I want.If you wanted to put an extracted bit actually on the page somewhere, you’d probably want to
cloneit:But looking at your comment to Juhana on the question:
…I wouldn’t load the additional comments at all (not in the HTML, and not in the script). Instead, I’d only load the comments I was going to show, and then have the “more” link fire off an ajax request for the rest of them if and when the user clicked it. You don’t gain much (if anything) from just moving them from the HTML into the script.
Here’s an example – live example | source:
HTML:
JavaScript:
What’s returned by the ajax call:
Re your comment below:
I don’t know what you mean by “…access the classes…”, but if you mean interact with the elements you got back without appending them, you just instantiate them, much as I did at the top of this answer with a literal string. For instance, do this in the
ajaxsuccess handler:Here’s the above example with different return data (a mix of
divelements, some with the classcommentand some with the classother), where I look at the data before appending it, and then only append the comments, not the “other”s: Live example | sourceHTML:
JavaScript (only change is in the
successfunction):Data returned by the
ajaxcall: