1

I am using the jquery touchwipe plugin on my scroll list, and i can't get
attribute from $(this).I want to use $(this) to get its children elment class="t7 edit" and add class 'show' to it.Does anyone know how to fix it?

html:

                <div id="main_list_wrapper">
                    <div class="item">
                        <div class="t7 edit"></div>
                        <div class="t8 cancel"></div>
                    </div>
                    <div class="item">
                        <div class="t7 edit"></div>
                        <div class="t8 cancel"></div>
                    </div>
                    <div class="item">
                        <div class="t7 edit"></div>
                        <div class="t8 cancel"></div>
                    </div>
                    <div class="item">
                        <div class="t7 edit"></div>
                        <div class="t8 cancel"></div>
                    </div>
                    <div class="item">
                        <div class="t7 edit"></div>
                        <div class="t8 cancel"></div>
                    </div>
                </div>

script code:

var $main_list_wrapper = $("#main_list_wrapper").find('.item');

$main_list_wrapper.touchwipe({
    preventDefaultEvents: false,
    wipeLeft: function() { 
        $(this).find('.t8.cancel').removeClass('show');
        $(this).find('.t7.edit').removeClass('show');
        var thisclass = $(this).attr('class');
        alert(thisclass);
        return false;
    },
    wipeRight: function() { 
        $sb(this).find('.t8.cancel').addClass('show');
        $sb(this).find('.t7.edit').addClass('show');
        return false;
    }
});

Like alert(thisclass). It shows "undefine".


Thank you all.my friend write this to me, and it work!

$main_list_wrapper.each(function () {
    var $this = $sb(this);

    $this.touchwipe({
        preventDefaultEvents: false,
        wipeLeft: function() { 
            var $pcs = $this;
            $pcs.find('.t8').removeClass('show');
            $pcs.find('.t7').removeClass('show');
            return false;
        },
        wipeRight: function() { 
            var $pcs = $this;
            $pcs.find('.t8').addClass('show');
            $pcs.find('.t7').addClass('show');
            return false;
        }
    });
});
Oliver
  • 39
  • 5
  • what do you mean by 'i can't get anything about $(this)' ? please elaborate. if you're asking what `$(this)` is, i think in this case it would be $main_list_wrapper. – kennypu Jan 08 '13 at 02:37
  • I want to use $(this) to get its children elment class="t7 edit" and add class 'show' to it.Thank you. – Oliver Jan 08 '13 at 02:49
  • 1
    `this` may be referring to the function `wipeLeft()`? – Mr. Polywhirl Jan 08 '13 at 02:49
  • I came across this post and thanks to your friend who wrote the above I have managed to get my swipe to work! Thanks so much :) – green_arrow Feb 05 '13 at 15:26

2 Answers2

0

Well, $("#main_list_wrapper") is NULL. Make sure that this snippet of code is located in your JQuery onLoad: $(document).ready(function () { /* CODE */ });

find() takes a list of class names delimited by commas.
Change: $(this).find('.t8.cancel') to this:$(this).find('.t8, .cancel')

Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
  • Yes,this snippet of code is located in my JQuery onLoad: $(document).ready(function() { /* CODE */ });The biggest problem is i can't get attribute from $(this) or to get its child elment.thank you. – Oliver Jan 08 '13 at 03:33
  • Well, @dunli stated earlier that the touchwipe wraps an object. You may only need native Javascript to access this inner object: `var element = document.getElementById('main_list_wrapper').children[0];` This may also work if you want to stick to JQuery: `element.children(":first");` – Mr. Polywhirl Jan 08 '13 at 03:42
0

$(this) refers to the object the function() belongs to, it is not referring to your $main_list_wrapper.

Refer to this post

You can just use $main_list_wrapper.find('.t8.cancel').removeClass('show'); instead.

Community
  • 1
  • 1
dunli
  • 1,356
  • 9
  • 18