javascript - Check if data attribute is not empty - Stack Overflow

I wish to map through all the td'scells in a table and check for their data attributes. If the at

I wish to map through all the td's/cells in a table and check for their data attributes. If the attribute/his assigned value is not empty, I would like to console.log it.

I got this for now but it does not seem to work properly(It just says that all of the td's are not empty). Also, I am not sure why the this inside the map function points to the window object but not to the exact td. Any ideas what am I missing?

function checkTds() {
    var $tab = $('table td');
    $.map($tab, function(){
        console.log($(this));
        if ($tab.attr("custom-data-attribute") !== "") {
            console.log($(this));
        }
    });
}

checkTds();

I wish to map through all the td's/cells in a table and check for their data attributes. If the attribute/his assigned value is not empty, I would like to console.log it.

I got this for now but it does not seem to work properly(It just says that all of the td's are not empty). Also, I am not sure why the this inside the map function points to the window object but not to the exact td. Any ideas what am I missing?

function checkTds() {
    var $tab = $('table td');
    $.map($tab, function(){
        console.log($(this));
        if ($tab.attr("custom-data-attribute") !== "") {
            console.log($(this));
        }
    });
}

checkTds();
Share Improve this question edited Oct 25, 2017 at 8:24 Mouser 13.3k3 gold badges30 silver badges54 bronze badges asked Oct 25, 2017 at 8:02 DevJoeDevJoe 4431 gold badge7 silver badges20 bronze badges 3
  • 2 you should be using $tab.map(), not $.map($tab) – billyonecan Commented Oct 25, 2017 at 8:05
  • 1 Both ways work. Since a nodelist can be iterated in jQuery you can also use $tab.map(). – Mouser Commented Oct 25, 2017 at 8:25
  • you can, but they're not the same. .map() is specifically for working with jquery collections – billyonecan Commented Oct 25, 2017 at 8:47
Add a ment  | 

1 Answer 1

Reset to default 4

You are using map which assigns its own variable to the iterated list:

From the documentation

callback Type: Function( Object elementOfArray, Integer indexInArray ) => Object The function to process each item against. The first argument to the function is the array item, the second argument is the index in array The function can return any value. A returned array will be flattened into the resulting array. Within the function, this refers to the global (window) object.

It is also standard to use the prefix data to make your custom attributes: data-«yourname».

function checkTds() {
  var $tab = $('table td');
  $.map($tab, function(element) {

    //look at the element var here
    //also check if the attribute exists!
    if ($(element).attr("custom-data-attribute") && $(element).attr("custom-data-attribute") !== "") {
      console.log($(element).attr("custom-data-attribute"));
    }
  });
}

checkTds();
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr>
    <td custom-data-attribute="1"></td>
    <td></td>
    <td></td>
    <td custom-data-attribute="4"></td>
  </tr>
</table>


On a side note: I personally would advise against using variables with the prefix $ while using jQuery. It makes it more easy to confuse them with actual jQuery functions.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745104193a4611459.html

相关推荐

  • javascript - Check if data attribute is not empty - Stack Overflow

    I wish to map through all the td'scells in a table and check for their data attributes. If the at

    13小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信