-2

I have some trouble in this code, I want to make a judge if the next().children() div hasn't lengh, then do an ajax require. But it always make an ajax require even the div has content.

Here is the html code:

<noscript>
<style>
.more{display:none;}
</style>
</noscript>
<!-- main content -->
<p class="click hidden" rel="12345">View more</p>
<div class="more">
    <h5>Relative post</h5>
    <div class="relative-post"></div>
    <!-- comment area -->
</div>
<!-- main content -->
<p class="click hidden" rel="23456">View more</p>
<div class="more">
    <h5>Relative post</h5>
    <div class="relative-post"></div>
    <!-- comment area -->
</div>

Here is the jquery code:

jQuery(document).ready(function(){
    $(".click").live('click',function() {
        if($(this).next('.more').is(':visible')){
            $(this).next('.more').css('display','none');
        }else{
            $(this).next('.more').css('display','block');
        }
        if($(this).next('.more').children('.relative-post:has(*)').length){
            }else{
            var $this = $(this);
            var item_id = $this.attr('rel');
            $.ajax({
                url: "process",
                dataType: "html",
                type: 'POST', 
                data: 'post=' + item_id, 
                cache: false,
                success: function(data){ 
                    $this.next('.more').children('.relative-post').html(data);
                }
            }); 
                    }
        }
    });
});
2
  • You can clean up your show/hide code by using jQuery toggle()... api.jquery.com/toggle Commented Aug 30, 2012 at 15:28
  • Btw live() method is deprecated. Use on(). Commented Aug 30, 2012 at 15:28

3 Answers 3

1
$(function() {
    $(document).on('click', '.click', function() {
        var next = $(this).next('.more');
        next.toggle(!next.is(':visible'));

        if (next.children('.relative-post:empty').length) {
            var item_id = $(this).attr('rel');
            $.ajax({
                url: "process",
                dataType: "html",
                type: 'POST',
                data: {post: item_id},
                cache: false,
                success: function(data) {
                    next.children('.relative-post').html(data);
                }
            });
        }
    });
});​

FIDDLE

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

2 Comments

Instead of children you may want to use contents. I up-voted though.
contents will get textnodes as well, but I don't see how I would use it here, as the :empty expression is what checks the element to see if it's empty, and that includes textnodes as well ?
0

Replace the code

if($(this).next('.more').children('.relative-post:has(*)').length)

with following

 if($(this).next('.more').children('.relative-post').html())

Comments

0
if ($(this).next('.more').children('.relative-post').html().length){

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.