This is a migrated thread and some comments may be shown as answers.

Button call javascript with parameters

1 Answer 917 Views
Toolbar
This is a migrated thread and some comments may be shown as answers.
Gilbert van Veen
Top achievements
Rank 1
Gilbert van Veen asked on 31 Aug 2017, 11:23 AM

Is it possible to call a Javascript with parameters when the user presses a button in the toolbar?

I can use the .Click() and the javascript is called but I can't figure out how to pass the parameters?

I als tried.....

 .Items(items =>
    {
        items.Add().Type(CommandType.Button).Text(Model.ToolbarSettings.NewButton.Text).Icon("plus")
            .Hidden(!Model.ToolbarSettings.NewButton.IsVisible).Enable(Model.ToolbarSettings.NewButton.IsEnabled)
            .HtmlAttributes(new { type ="button", Id = "aa" })
            .Click("onClick")

But how can I read the Id value in the javascript?

I want pass two parameters to the script (action/controller)......

 

1 Answer, 1 is accepted

Sort by
0
Stefan
Telerik team
answered on 04 Sep 2017, 06:01 AM
Hello Gilbert,

In general, I can suggest setting the ID using the ID property of the button, then it will be available in inside the click event under e.id:

items.Add().Type(CommandType.Button).Text(Model.ToolbarSettings.NewButton.Text).Icon("plus")
           .Hidden(!Model.ToolbarSettings.NewButton.IsVisible).Enable(Model.ToolbarSettings.NewButton.IsEnabled)
           .HtmlAttributes(new { type ="button", Id = "aa", customAttr = "test"})
           .Id("aa")
           .Click("onClick")

Then any other custom attributes can be retrieved inside the event using jQuery and the e.target element:

function onClick(e) {
        console.log($(e.target).attr("customAttr"))
        console.log(e.id)
}

I hope this is helpful.

Regards,
Stefan
Progress Telerik
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
Toolbar
Asked by
Gilbert van Veen
Top achievements
Rank 1
Answers by
Stefan
Telerik team
Share this question
or