1

How do I find a certain attribute exists or not for a selected item in jQuery?

For example, selected element jQuery('.button'), now this can have an attribute 'xyz'. How do I get whether it is having that or not?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Alice
  • 1,422
  • 1
  • 18
  • 30
  • Please put what code you have tried so far. And please go through following link for how to ask question on SO https://stackoverflow.com/help/how-to-ask – Nilesh Mahajan Apr 17 '17 at 06:55
  • thanks @rajesh, you have given the exact link, that hasAttr worked perfectly. Thanks again ;) – Alice Apr 17 '17 at 06:59

2 Answers2

7

You can use the hasAttribute method:

// Attach the event to the button
$('button').on('click', function() {
  // This will refer the element from where event has originated
  if (this.hasAttribute("style")) {
    alert('yes')
  } else {
    alert('no')
  }

})

If you want to use the jQuery library, you can directly use the attr method in the if condition.

if($('#yourElement').attr('someProp')){}

Alternatively, you can also use the jQuery is selector:

if ($(this).is('[style]')) {// rest of the code}

DEMO

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
brk
  • 48,835
  • 10
  • 56
  • 78
  • Should we really answer a dupe? – Rajesh Apr 17 '17 at 06:57
  • 1
    @Rajesh Why not? :) more good answers won't hurt. – dfsq Apr 17 '17 at 06:59
  • @dfsq Good answer never hurt, but scattered answer do. If its a good answer and is not posted on dupe, you should close and post this answer on original post. Since that post is going on for long time, you will get different options and discussions in comment that covers a lot more information. – Rajesh Apr 17 '17 at 07:02
  • @dfsq This might help: https://meta.stackexchange.com/a/10844/351737 – Rajesh Apr 17 '17 at 07:04
  • @Rajesh Well I have different opinion about SO and its purpose. People just want reputation so they answer questions, duplicates easier to answer, so why not. But whatever really. – dfsq Apr 17 '17 at 07:14
  • Lets not debate, but *People just want reputation* is a little wrong assumption (*though not wrong completely*). At least for few like me. I'd prefer closing and even deleting my answer to a dupe. Reps are important, but main reason is to help as we will also seek it someday. But this is just my POV. – Rajesh Apr 17 '17 at 07:18
  • I found ($(this).is() is exact thanks dfsq – Alice Apr 17 '17 at 08:39
3

You have two solutions that I know of. 1) Native Javascript has a function for this .hasAttribute() (e.g. $(this)[0].hasAttribute('name');). 2) Using jQuery, you can check if $(this).attr('name') is undefined or not.

Chris Thorsvik
  • 450
  • 7
  • 15
  • Please make a note that its a bad practice to answer a duplicate. If a question looks very basic or straight forward, there is a high possibility, its been asked before. You should search for such posts and share their link as comment (*until you do not have privilege to close, and then close yourself*) – Rajesh Apr 17 '17 at 07:00