-2

I have a table with attr which I don't know how to get:

<ul id="myTable" >
    <li><span data-myData="01">first value</span></li>
    <li><span data-myData="02">second value</span></li>
</ul>
<button id = "myButton"> click me </button>

in jQuery, when element is selected and clicking the button:

$(document).on('click', '#myButton', function() {  
 var tableLength = $('#myTable li').length;
 for(var m=0; m<=tableLength; m++){
     if($('#myTable li' + ":nth-child(" + (m+1) + ")").hasClass('selected')){
        var valor = $('#myTable li' + ":nth-child(" + (m+1) + ")").text(); //works
        var id = $('#myTable li' + ":nth-child(" + (m+1) + ")").attr("data-myData"); // doesn't work
  
     }
   } 
});

How can I get the value?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
cucuru
  • 3,456
  • 8
  • 40
  • 74

6 Answers6

1

With this #myTable li' + ":nth-child(" + (m+1) + ") you select the li not the span. The jquery text function works on li, actually it rewrite the li's content and remove the span inside.

Change this#myTable li' + ":nth-child(" + (m+1) + ")" into this #myTable li' + ":nth-child(" + (m+1) + ") span"

1

The main issue with your code is that the data attribute is on the span, not the li, hence your code returns undefined.

Also note that you can tidy your code up with the use of each() and the :gt selector to avoid the first li in the ul. Try this:

$(document).on('click', '#myButton', function() {
  $('#myTable li:gt(0)').each(function() {
    var $li = $(this);
    var valor = $li.text();
    var id = $li.find('span').data('mydata'); // note data() and lowercase property name
    
    console.log(valor);
    console.log(id);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<ul id="myTable">
  <li><span data-myData="01">first value</span></li>
  <li><span data-myData="02">second value</span></li>
  <li><span data-myData="03">third value</span></li>
</ul>
<button id="myButton">click me</button>
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
0

Firstly : It's not a table. UL is a list.

Secondly : Simple search, simple answer : Get the pure text without HTML element by javascript?

Thirdly : Keep your JS simple!

var allSpan = $('#myTable li span') // Contain all span
allSpan.each(function() { console.log(this.innerHTML+ " | "+ this.getAttribute("data-myData")) });
Community
  • 1
  • 1
Aks
  • 395
  • 3
  • 11
0

First of all it is not a table , it is <ul> ,Please find below the snippet and change attribute to lowercase as in snippet

$('span').click(function() {
    alert($(this).data('mydata'));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<ul id="myTable">
  <li><span data-mydata='01'>first value</span></li>
  <li><span data-mydata='02'>second value</span></li>
  <li><span data-mydata='03'>third value</span></li>
</ul>
<button id="myButton">click me</button>

Same you can use for button

Mandeep Singh
  • 1,287
  • 14
  • 34
0

You can use .map

 $("#myTable li span").map(function() {
    var a = $(this).attr("data-myData");
    alert(a);
 }).get();
A. Jain
  • 167
  • 8
0

use this code .it will work definetly:

   $(document).on('click', '#myButton', function() {  
     $.each('#myTable li',function(i,v){
       if($(this).hasClass('selected'))
            {
                 var valor = $(this).find('span').text();
                 var id = $(this).find('span').attr('data-myData');
            }

     });
  });