Assign a custom function to form button

Assign a custom function to form button

by Viktor Merinov -
Number of replies: 7

I have the form 'usertests' with three fields: 'Competency name', 'Course','Test' and two buttons: 'Submit' and 'Add test'. I need to assign a custom function('add_test()') to the button 'Add test'. When a user clicks the 'Add test' button two new fields('Course' and 'Test') are added to the form. I do not understand how to assign custom function to the button. Anybody knows?

Average of ratings: -
In reply to Viktor Merinov

Re: Assign a custom function to form button

by Mark Johnson -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Hi Viktor, Take a look at repeat elements, I think that shoud do what you want.

In reply to Mark Johnson

Re: Assign a custom function to form button

by Viktor Merinov -
Hi Mark! Thanks for your answer. I know about 'repeat elements'. I can make it. it is not clear to me exactly how to associate an event with a button that calls the function of creating elements. I do not understand how to implement the button itself. Do you understand me???
In reply to Viktor Merinov

Re: Assign a custom function to form button

by Mark Johnson -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

It's been a while since I've done it myself, but I dont think you need to create the button. If you add at $this->repeat_elements() to your form defintion, it will create a "Add xx more" button for you.

In reply to Mark Johnson

Re: Assign a custom function to form button

by Viktor Merinov -
What about a button for deleting added fields??
In reply to Viktor Merinov

Re: Assign a custom function to form button

by Mark Johnson -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Hmm, I'm not sure about that. I know that empty repeated elements should be ignored, but I dont know about removing them once they've been added.