Denis Stephanov
Denis Stephanov

Reputation: 5231

jQuery onclick event not works

I have a lot of buttons with specific class like this:

enter image description here

All of these buttons has stored JSON in data attribute. I've created function, where I detect clicked button, and do some stuff with this JSON like this:

$(".btn-load-road").on("click", function () {
    console.log($(this).data("route"));
});

Click event is not fired. Can you tell me what is wrong with this?

Upvotes: 0

Views: 52

Answers (2)

ameave
ameave

Reputation: 426

I see a problem in the quotation of the "data-route" value. You are trying to double-quot a string that is already doublequotted...

Instead of

data-route=""coordinates":[[18.159425,49.835919],...],"type":"LineString"}"

You could try single-quot the quoted strings inside your main string

data-route="{'coordinates':[[18.159425,49.835919],...],'type':'linestring'}"

See this working Fiddle

Upvotes: 0

HenryDev
HenryDev

Reputation: 4953

You can use:

prop

method to get the value of data-route (JSON value)

Here's a quick solution. Hope it helps!

$(".btn-load-road").on("click", function () {
console.log($(this).prop("data-route"));
});

Upvotes: 1

Related Questions