1

I have been looking at some other examples, but they're not quite the same unfortunately. I am trying to delete some data onclick (omitted that part here, only need the id for that). Problem is that it returns undefined for the id I want.

 function deleteMessage(){
    var $currentId = $(this).closest(".ulrecords").attr('id');
    alert("ID: " + $currentId)
 }

The method is being called as follow

<a href="javascript:deleteMessage();" id="amessage">

And the HTML looks like this

<ul class="chat-messages">
  <ul id="msg_1234567890_a1bs2e" class="ulrecords">
   <li>
     John
     <span>27 minutes ago</span>
      <a href="javascript:deleteMessage();" id="amessage"> X</a>
    </li>
    <li id="limessage">
       Some message here
    </li>
  </ul>
  <ul id="msg_9234742878_73bhad" class="ulrecords">
   <li>
     John
     <span>28 minutes ago</span>
      <a href="javascript:deleteMessage();" id="amessage"> X</a>
    </li>
    <li id="limessage">
       Some other message here
    </li>
  </ul>
</ul>

So when i click on the in one of the ul's I want to obtain that Hope anyone can point me in the right direction

dnsko
  • 1,017
  • 4
  • 17
  • 29

4 Answers4

5

Because this is window in your case.

Either pass in the reference to the element you clicked on

<a href="javascript:deleteMessage(this);" id="amessage">

and

function deleteMessage(elem){
    var $currentId = $(elem).closest(".ulrecords").attr('id');
    alert("ID: " + $currentId)
}

or better yet, attach events with eventListeners.

$(document).on("click", ".ulrecords", function () {
    alert(this.id);
});

or

$(document).on("click", ".ulrecords a", function () {
    var $currentId = $(this).closest(".ulrecords").attr('id');
});

Also ids are singular.

epascarello
  • 204,599
  • 20
  • 195
  • 236
0

You're calling the method through an href link, which does not set the execution context for the method to be the element clicked. In order to get the correct element, don't call the method through href, but rather attach a click handler to it. Now, your anchors have duplicate IDs, which is a whole lot different issue that you need to fix.

I'm putting a class to all your anchors in the example to select them easily with jQuery.

<a href="javascript:deleteMessage();" class="my-button">X</a>

And JavaScript:

$(function () {
    $(document).on("click", ".my-button", function () {
        var $currentId = $(this).closest(".ulrecords").attr('id');
        alert("ID: " + $currentId);
    });
});
Konstantin Dinev
  • 34,219
  • 14
  • 75
  • 100
0

$(document).ready(function() {
  $('.delmsg').click(function() {
   alert($(this).parent().parent('ul').attr('id'));
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="chat-messages">
  <ul id="msg_1234567890_a1bs2e" class="ulrecords">
   <li>John <span>27 minutes ago</span> <a href="javascript:deleteMessage();" class="delmsg">X</a></li>
   <li id="limessage">Some message here</li>
  </ul>
  <ul id="msg_9234742878_73bhad" class="ulrecords">
   <li>John <span>28 minutes ago</span> <a href="javascript:deleteMessage();" class="delmsg">X</a></li>
   <li id="limessage">Some other message here</li>
  </ul>
</ul>
Nick De Jaeger
  • 1,247
  • 10
  • 13
0

$(function(){
  $('.remove').on('click', function(){
    var currentId = $(this).closest(".ulrecords").attr('id');
    console.log(currentId);
  });  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id="msg_1234567890_a1bs2e" class="ulrecords">
  <li>
    John
    <span>27 minutes ago</span>
    <a href="javascript:void(0);" class="remove"> X</a>
  </li>
  <li id="limessage">
    Some message here
  </li>
</ul>
<ul id="msg_9234742878_73bhad" class="ulrecords">
  <li>
    John
    <span>28 minutes ago</span>
    <a href="javascript:void(0);" class="remove"> X</a>
  </li>
  <li id="limessage">
    Some other message here
  </li>
</ul>
itzmukeshy7
  • 2,669
  • 1
  • 21
  • 29