0

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?

2 Answers 2

1

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"));
});
Sign up to request clarification or add additional context in comments.

1 Comment

sorry I edit question, I mean function "on", It is on title.
0

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

1 Comment

sorry I edit question, I mean function "on", It is on title.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.