How to get element above Jquery?

4

I have the following elements:

<p class="id-task">5</p>
<p class="status-change">
    <i id="test-task" class="fa fa-flask fa-lg" aria-hidden="true"></i> 
</p>

I'm trying to get the number of <p class="id-task">5</p> with the following Jquery code:

$('#test-task').on('click', function(){ 
    var father = $(this).parent().siblings();
    var idTask = $(father).text();
    alert(idTask);  
});

I changed the code, because the parent() end has to be .siblings() , because <p class="id-task">5</p> is brother of <p class="status-change"></p>

    
asked by anonymous 02.12.2016 / 17:20

3 answers

4

The prev () method returns the previous element.

$('#test-task').on('click', function(){ 
    var element = $(this).parent().prev();
    var idTask = $(element).text();
    alert(idTask);  
});
    
02.12.2016 / 17:27
3

Change to $(father).text(); , you're catching the whole html.

$('#test-task').on('click', function(){ 
    var father = $(this).parent().parent();
    var idTask = $(father).text();
    alert(idTask);  
});
    
02.12.2016 / 17:23
2

Just pick up the class.

$('.id-task').text();

JS

$('#test-task').on('click', function(){ 
    var idTask = $('.id-task').text();
    alert(idTask);  
});
    
02.12.2016 / 17:25