I want to get key name from my html element?
Sample code :
<td data-code="123">220</td>
Using jquery data method I am able to key value but I want to extract key name?
var keyValue=$("td").data("code"); //123
var keyName=?????
You can access all data keys and values this way:
$.each($("td").data(), function(key, value) {
console.log(key + ": " + value);
});
HERE is the example.
data-code would be the key for that.
If you want to get the keys for unknown key/value pairs you can use a for (var key in data) {}
loop:
var all_values = [],
data = $('td').data();
for (var key in data) {
all_values.push([key, data[key]]);
}
//you can now access the key/value pairs as an array of an array
//if $(td).data() returns: `{code : 123}` then this code would return: [ [code, 123] ]
//you could get the first key with: all_values[0][0] and its corresponding value: all_values[0][1]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With