E
E
EVOSandru62015-10-11 04:11:55
JavaScript
EVOSandru6, 2015-10-11 04:11:55

How to nicely access parent this in jquery?

A snippet like this:

$('.exapmle-1 input').keyup(function()
{
   var value = $(this).val();
   $(this).parent().parent().find('td:gt(0)').each(function()
   {
       console.log(value);
   });
});


Is it possible instead of console.log(value); refer to the parent's this otherwise, without creating a value variable ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nazar Mokrinsky, 2015-10-11
@EVOSandru6

It is possible, but it will not be better:

$('.exapmle-1 input').keyup(function()
{
   var f = function() {
       console.log($(this).val());
   };
   $(this).parent().parent().find('td:gt(0)').each(f.bind(this));
});

It’s nicer to write such things in CoffeeScript:
$('.exapmle-1 input').keyup ->
    $(this).parent().parent().find('td:gt(0)').each =>
        console.log $(this).val()
        return

Or LiveScript:
$('.exapmle-1 input').keyup ->
    $(this).parent().parent().find('td:gt(0)').each !~>
        console.log $(this).val()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question