Is there a way that I can check if a data attribute exists?
if ($("#dataTable").data('timer')) {
...
}
NOTE this only returns true
if the data attribute is not empty string or a "falsey" value e.g. 0
or false
.
If you want to check for the existence of the data attribute, even if empty, do this:
if (typeof $("#dataTable").data('timer') !== 'undefined') {
...
}
How to check if data attribute exist with plain javascript?
Element.getAttribute
returns null
or empty string if the attribute does not exist.
You'd use Element.hasAttribute
:
if (!object.hasAttribute("data-example-param")) {
// data attribute doesn't exist
}
or Element.dataset
(see also: in
operator):
if (!("exampleParam" in object.dataset)) {
// data attribute doesn't exist
}
or even
if (!object.getAttribute("data-example-param")) {
// data attribute doesn't exist or is empty
}
Check if data attribute exists without a value
Through jQuery you can use the .is(selector)
method.
So if you set the selector to an attribute one, you can do your check
$(function() { $('div').click(function() { if ($(this).is('[data-specs]')) { console.log('has specs'); } else { console.log('no specs'); } });});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><div data-specs>Specs</div>
checking to see if data attribute has a value or not
First as pointed out in comments, you are missing an equal sign so you are not doing a comparison. Second issue is that if there is no data attribute it is undefined
, not null
.
if ($('#myImg').data('tablet')===undefined) {
Find if HTML data attribute exists in any ancestor elements
I want to see if there is a more efficient, more eloquent, or easier-to-code way of doing this.
There certainly is. Meet closest()
let ancestor = baseElement.closest('[html-attribute]');
let attr_val = ancestor ? ancestor.getAttribute('html-attribute') : null;
closest()
takes a selector, just like querySelector
and similar methods.
Check for existence of specific data attribute value
try this:
if ($('.item[data-index="' + indexVal + '" ]').length > 0){
}
Jquery check element exists by data attribute matching only the start of a specific value
here is how
if ($('[data-attr^="123"]').length > 0) {
//do something
}
Javascript - On click, check if element has an attribute with value
Use e.target.getAttribute
to get the attribute value of the target element. Then compare that with the value of the attribute you expect in the if
condition:
document.body.addEventListener('click', function(e){ console.log(e.target); if(e.target.getAttribute('data-a-target') === "popout-chat-button"){ console.log('popout button'); }});
<button data-a-target="popout-chat-button" data-test-selector="popout-button">Popout</button>
Related Topics
Write to a Text or Json File in React With Node
Javascript to Hide Multiple Div
Update Url With Value from Input on Click With React
Jquery on Click Function Not Working With Bootstrap
Php - Return Confirm Within PHP Issue
415 (Unsupported Media Type) With Rest Post Request
Check If Value Select2 Is Null
How to Select Id and Get a Value of It in Input Data in This Row
How to Expire a Cookie in 30 Minutes Using Jquery
Javascript (+) Sign Concatenates Instead of Giving Sum of Variables
Best Way to Get Objects With Highest Property Value
How to Make Any Hovered Color Darker/Lighter That the Original
Vue.Js - How to Concat One Key Value to Another Key'S Value
Expanding Only One Row When Button Clicked
Using Js Remove Comma from Number from Existing Data
How to Merge the Property With Same Key in Two Object Array
Combining JavaScript and CSS into HTML File
How to Execute a JavaScript Function from the Java Part of Jsp