user6902601 user6902601 - 2 months ago 10
Javascript Question

How to use $(this) inside JS function and then find attr?

How do you use

$(this)
with a JS function and then find the
attr
?

function date_box() {
alert(this.getAttribute(week));
}


I want to get the attribute
week
from an element called
gospel_table
4. On click of
gospel_table4
, the function is triggered:

<a href='#'><div id='gospel_table4' week='$one_date' onclick='date_box()'> Week $one_date </div></a>


I can't do this:

$(this).click...


Because only one element of
gospel_table4
is clickable. With the other method, all the elements of
gospel_table4
are clickable.

Basicially, how do I get the attr from the function of date_box()?

Answer

You need a param inside your function

HTML:

<a href='#'><div id='gospel_table4' week='$one_date' onclick='date_box(this)'> Week $one_date </div></a>

Javascript:

function date_box(thisdiv){
   alert($(thisdiv).attr("week"));
}