0

so I am having a problem i dont know how to solve it, I spent hours just trying to find a way to get my value of my element.

Here is my code (jsp) :

<div class="aimerSection">
     <div class="aime">
     <form>
     <input type="hidden" class="adore1" name="aime" value="${post.id}">
     <input type="button" class="adore2" value="J'aime">
     </form>
     </div>
     <c:if test="${ post.adore == 0 || post.adore == 1 }">
     <div class="nbreAimes"><p><span class="nbrAdore">${ post.adore }</span> personne aime ça</p></div>
     </c:if>

     <c:if test="${ post.adore != 0 && post.adore != 1 }">
     <div class="nbreAimes"><p><span class="nbrAdore">${ post.adore }</span> personnes aiment ça</p></div>
     </c:if>

</div>

What i want to achieve is to get the value of my 'p' in the div with 'nbreAimes' when i click my button ( input type button ) so that's my jquery file :

$(document).ready(function(){
$(".adore2").click(function(){
    var aime = $(this).parent().find(".adore1").val()
    var value = $(this).parent().parent().siblings().find(".nbrAdore").text()
    alert(value)

    console.log("value : "+$(this).parent().parent().siblings().find("p").text());
    $.ajax({
        type:"POST",
        data: {aime:aime},
        url:"acceuilServlet",
        success:function(result){

            console.log("in functions : "+$(this).parent().parent().siblings().find("p").text());
        }
    })

})
})

But the problem is that I am getting the value correctly, but inside the function of ajax ( success : function ) i copied exactly the same code and I get nothing ..

in my console I see :

value : 1 personne aime ça 
in functions : 

i get Nothing.

Hope I am going to find some help. Thank you.

TaouBen
  • 1,165
  • 1
  • 15
  • 41
  • 3
    Possible duplicate of [$(this) inside of AJAX success not working](https://stackoverflow.com/questions/6394812/this-inside-of-ajax-success-not-working) – Amr Elgarhy Jan 07 '18 at 20:38
  • Hello! Can you rewrite your as an Answer so I can mark it as a right answer! it worked :D. Have a blessed day. – TaouBen Jan 07 '18 at 20:48
  • 1
    great that it worked, actually my comment is not an answer it is just a flag that your question is an exact duplicate of another question, so you can upvote the referenced question and answer if it helped you. – Amr Elgarhy Jan 07 '18 at 20:51

1 Answers1

1

The $(this) inside AJAX is different from the one outside. Try to save your value in another variable outside the AJAX call for later usage inside AJAX.

$(document).ready(function(){
$(".adore2").click(function(){
    var aime = $(this).parent().find(".adore1").val()
    var value = $(this).parent().parent().siblings().find(".nbrAdore").text()
    alert(value)

    console.log("value : "+$(this).parent().parent().siblings().find("p").text());

    var pText = $(this).parent().parent().siblings().find("p").text()

    $.ajax({
        type:"POST",
        data: {aime:aime},
        url:"acceuilServlet",
        success:function(result){

            console.log("in functions : "+pText);
        }
    })

})
})
T.Liu
  • 492
  • 1
  • 5
  • 16