1

I have html table like this

<td id="18" data-class-index="0" data-day=2017/11/18>18</td>
<td id="19" data-class-index="0" data-day=2017/11/19>19</td>
<td id="20" data-class-index="0" data-day=2017/11/20>20</td>

I have data-day like date=2017/11/18. I would like to get id number

I tried like

$("id[data-day='2017/11/20']")

But it didn't work well.I know it is basic question, but if someone has some idea,Please let me know.

3
  • 2
    you can use $("td[data-day='2017/11/20']").attr('id'); Commented Dec 20, 2019 at 5:29
  • thanks,If I have variable date =2017/11/20, How can I achieve it ? I treid $("td[data-day="+data+"]").attr('id') but some error has occured. Commented Dec 20, 2019 at 5:54
  • i have added answer plz check Commented Dec 20, 2019 at 5:56

4 Answers 4

2

Using string literal and querySelector():

let data = "2017/11/18";
let id = document.querySelector(`td[data-day= "${data}"]`).id;
console.log(id)
Sign up to request clarification or add additional context in comments.

Comments

1

You can try this one.

$(document).ready(function(){
    var val = $('td').filter(function(index){
        return $(this).data('day') == '2017/11/19';
    }).attr('id');
    console.log(val);
});

Comments

1

console.log($('table td[data-day="2017/11/20"]').attr('id'));
console.log('__DEBUG__')
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
 <table>
<td id="18" data-class-index="0" data-day="2017/11/18">18</td>
<td id="19" data-class-index="0" data-day="2017/11/19">19</td>
<td id="20" data-class-index="0" data-day="2017/11/20">20</td>
<table>

1 Comment

Thanks,I would like to use variables,like date = 2017/11/20 and so on, How can I use such variables?
-2

Use the .data() method to access it directly

var value = $("#20").data("day");

See https://api.jquery.com/jQuery.data/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.