Micah
Micah

Reputation: 4560

JS determine if event.target has "[ref=xxxx]"

Please advice the way to know that if the event.target is this element which has ref="xxxx"

I tried,

if(event.target.hasAttribute("[ref=xxxx]"))

, it didnt work.

Thank you very much.

HTML

<div ref="xxxx"> xxxxxxxxxxxxxxxxx</div>

Upvotes: 1

Views: 578

Answers (3)

charlietfl
charlietfl

Reputation: 171689

hasAttribute() will return a boolean and syntax is hasAttribute( attributeName)

You want getAttribute()

if(event.target.getAttribute("ref") =="xxxx")

Upvotes: 1

Andrew Hubbs
Andrew Hubbs

Reputation: 9436

Since you are using jQuery (based on question tags), you could do something like:

if ($(event.target).is("[ref='xxx']")) {  // or possibly $(this)...
  //...
}

Upvotes: 3

mkoryak
mkoryak

Reputation: 57968

if you are using jquery, you should use jquery :)

test if the target's ref attribute is 'xxxx'

if($(event.target).attr("ref") === "xxxx"){

}

or, see if it actually has a ref attribute:

if($(event.target).filter("*[ref]").length){

}

Upvotes: 5

Related Questions