Data Attribute Setting

Hello Experts,

For some reason the code below always set the "haus" variable to "street 003" , no matter if the data-haus is set in the area to "4".
Can you have a look and correct me?

<area shape="poly" id="unit15" coords="7,167,6,268,216,264,216,167" href="#" data-haus="1" data-sqm="200.01">

if ($("area").data('haus') === "1") {
		var haus = "Street 003";
} else { 
		var haus = "Street004";
};

Open in new window

RefaelAsked:
Who is Participating?
 
Chris StanyonCommented:
Your comparison operator is checking that the data-haus is the same value AND the same type (===). That will return false because your data-haus attribute is a number, but this checks if it's a string:

// check for a string with a value of 1
if ($("area").data('haus') === "1") {

Either change the operator to double equals (==) or change your if statement to compare against a number:

// Check for a value of 1
if ($("area").data('haus') == "1") {

or

// Check for a number with a value of 1
if ($("area").data('haus') === 1) {
0
 
Brian TaoSenior Business Solutions ConsultantCommented:
It looks fine.  I guess the issue may resides where you set data-haus to 4.  Can you show that part?
0
 
RefaelAuthor Commented:
Brian Tao, Thank you.

Right after the script above I have the following script. The "qtip" is a tooltip plugin and i am passing the variable "haus" to the tooltip.

jQuery("area").each(function() {	
    jQuery(this).qtip({
        content: {
            text: 'haus',
       },
.............

Open in new window

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Brian TaoSenior Business Solutions ConsultantCommented:
That's not what I meant.  You're USING haus here.  Where do you SET the data-haus attribute? It's literally "1" in your first post.
0
 
RefaelAuthor Commented:
Brian Tao I am not sure what you mean. The area shape html I posted above. Here it is again.

<area shape="poly" id="unit15" coords="7,167,6,268,216,264,216,167" href="#" data-haus="1" data-sqm="200.01">

Open in new window

0
 
Brian TaoSenior Business Solutions ConsultantCommented:
Yes, you have data-haus="1" in it.  So why are you complaining the if statement always setting haus = "Street 003"?
The logic in your code says if data-haus is "1" then haus = "Street 003".  Since it IS "1", of course haus is always "Street 003"!
0
 
RefaelAuthor Commented:
Brian Tao,

This was one example, I have more more code on the page and many have the "haus" data equal to different values .
I think you did not read the question.
0
 
Brian TaoSenior Business Solutions ConsultantCommented:
Well I did.  That's why I was asking for the part of code where you set the "haus" data to different values.  Or better yet, paste the entire code or provide a link to your page.
0
 
RefaelAuthor Commented:
Hello Brian Tao,

I had to add the "this" to the "each" function so it will refer to each "area"....  I did not notice that!
So after the "each" function, in the if statement refer to the "this" :-)

" if($(this).data('haus') === "1") { }"
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.