Select to view content in your preferred language

Having a button pass a variable into a function

4752
6
07-06-2015 05:52 PM
HamishKingsbury1
Occasional Contributor

Hi all

What I'm trying to do is when a list of points are added to a map, they are also added to a list which is displayed to the user. What I want to be able to do is beside each of these items is have a 'delete' button which will pass the button ID into a function. I have got all the code figured out apart from passing a unique variable into a function to delete the correct point. Below is the code I have so far:

This is the code which creates a delete button with a unique ID (basically i want that id passed into the function)

string = '<p>' + consentLocattr[1] + '<button id='+i+' data-dojo-attach-event="onclick:delete('+i+')">Delete</button></p>';

And here is the function (at the moment i'm just trying to get it to work)

delete: function(item){
alert(item);

},

Cheers, and sorry about the formatting, it doesn't seem to like me currently

Hamish

Message was edited by: Hamish Kingsbury

Tags (1)
0 Kudos
6 Replies
RobertScheitlin__GISP
MVP Emeritus

Hamish,

  My suggestion is not to pass anything in the delete function call from onclick and in the actual delete function use

delete: function(evt){
    console.info(evt.target.id);
}
HamishKingsbury1
Occasional Contributor

Hi Robert, I tried that, changing the string to

string = '<p>' + consentLocattr[1] + '<button id='+i+' data-dojo-attach-event="onclick:delete()">Delete</button></p>';

and the function to what you suggested, however nothing comes up in the console. Not even an error

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Hamish,

   OK, data-dojo-attach-events are ONLY for items that are exiting in the template html and will not be used for items that are dynamically added via code. So when you add string var from your code are you using domConstruct to create the actual dom elements?

HamishKingsbury1
Occasional Contributor

I'm not really sure sorry.... I'm not that skilled in JS.... If I understand correctly, this is how i'm doing it...

string = '<p>' + consentLocattr[1] + '<button id='+i+' data-dojo-attach-event="onclick:delete()">Delete</button></p>';
document.getElementById("current").innerHTML = document.getElementById("current").innerHTML + string;
0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Hamish,

   OK that is the problem. You need to look into dojo/dom-construct for building dom elements dynamically like you are doing especially if you wan to attach event.

HamishKingsbury1
Occasional Contributor

Okay, thank you!

0 Kudos