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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

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
Chris StanyonWebDevCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.