Skip to content

Instantly share code, notes, and snippets.

@khoand0000
Last active October 28, 2023 17:02
Show Gist options
  • Save khoand0000/d579506c622fe6c00cd2 to your computer and use it in GitHub Desktop.
Save khoand0000/d579506c622fe6c00cd2 to your computer and use it in GitHub Desktop.
  • 1st way (I don't like it, it's long) but it suits for checking object has the field or not (2nd way is not applied)
var attr = $(this).attr('name');

// For some browsers, `attr` is undefined; for others, `attr` is false. Check for both.
if (typeof attr !== typeof undefined && attr !== false) {
  // Element has this attribute
}
  • 2nd way: native javascript (love it)
$(this)[0].hasAttribute("name"); // love it, it's short

jQObject[0].hasAttribute("name");
  • 3rd way
$(this).is('[name]');

$(this).filter("[name='choice']");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment