-3

If i have html like this.Is there a way get a text apple between < div class="a" > and send it trought ajax to gwt application ?

<div class="A">
   <div class="B">
      <img class="icon" src="/images/ico.png" alt="" />
      <div class="a">apple</div>
      <div class="b">bannana</div>
   </div>
</div>

and i have JavaScript like this :

$function(){
    $('.B .icon').click(function(){

        $(this).closest('.B').addClass('marked');
    });
}
user3416113
  • 61
  • 1
  • 10
  • you need to get the text apple on jQuery ? –  Jan 27 '15 at 20:47
  • 1
    what does it have to do with GWT? – yurgis Jan 27 '15 at 20:50
  • yes, i need to get the text apple. And sorry, i was wrong about adding GWT – user3416113 Jan 27 '15 at 20:58
  • And what on earth does adding a class do to help you get some text from an element? If someone on the street stopped you and asked you this question, you wouldn't know what they are talking about. – JK. Jan 27 '15 at 21:03
  • adding a class is for other things.. it doesn't halp get text from an element. its just my method, in which i would like to get that apple text. – user3416113 Jan 27 '15 at 21:07

1 Answers1

1

I would add this as a comment if I had enough reputation as I'm confused about your structure and question in general. Apologies if I don't understand correctly, but if you're typically traversing back up the DOM structure to the parent in your case could you use find to grab the element? .html() will grab the current content of said element.

$(function() {
    $('.icon').click(function(){
        var html = $(this).closest('.B').find('div.a').html();
        // Do what you want with the contents. Simple alert as example.
        alert(html);
    });
});

This should get you what you're looking for. Please mark it as the answer if you find it matches what you need.

I'd suggest using .parent() rather than .closest('.B') if your elements will always be in the order you've posted and if further searching of the ancestor elements isn't needed.

CannonFodder
  • 332
  • 2
  • 11
  • No problem. If your structure will always match the HTML you've posted I think you may be better using .parent(). That's me assuming you may not have used .closest() intentionally. http://stackoverflow.com/a/10499468/2527231 – CannonFodder Jan 27 '15 at 21:31
  • i noticed that i cant send that var html with AJAX GET method. why does that happen ? – user3416113 Jan 28 '15 at 20:11