How can I add "on click" event from external js file

How to add an onclick event from an external JavaScript file in ChronoForms.

Overview

The issue occurs because the JavaScript code in the external file is not properly attached to the button element, even though the file loads correctly.
Ensure the external JavaScript file uses the correct jQuery syntax to bind the click event, and verify that the button's name attribute matches the selector in the code.

Answered
hg hgaleano 11 Jun, 2015
Answer
Hi everyone
I’m new in chronoforms, I’m working with CFV5 and Joomla 3.3. I need to add a click event to a button, I found a “how to” and I have added a load javascript action with the code below:
jQuery(document).ready(function($)
{
function addCmdTestClick()
{
$(document).on('click', 'input[name="cmdTest"]', function(){ 
 alert('click');
});
}
addCmdTestClick();
});


It works perfectly, but now, I need to save this code in external js file and load it with
<?php
JFactory::getDocument()->addScript(JURI::base().'code/MyJS.js');
?>


My external file looks like this
function cmdTest1Click()
{
	alert("CLICK from file")
}

function addCmdTest1Click()
{
	alert("File Loaded OK");
/* just one of these */
	$(document).on('click', 'input[name="cmdTest1"]', function(){alert("click");});
	$(document).on('click', 'input[name="cmdTest1"]', cmdTest1Click);
	$(document).on('click', 'input[name="cmdTest1"]', cmdTest1Click());
}

addCmdTest1Click();
I have made a lot of tests but nothing works, I know that the file is loaded correctly, but the click event is not triggered.

Is possible to do that from js file?
If it is possible, what changes I have to make in my external file?

Thanks in advance
Hugo G
This topic is locked and no more replies can be posted.