Questions and discussion about using ProcessMaker: user interface, running cases & functionality
By Bosatzu
#821959
Hello everyone

I'm trying to use onload javascript function on a daynaform but i can not make it works, i hope you can help me out.

Here is my code:
Code: Select all
$("document").ready( function() {
  location();
  tipoUsuario();
  
  $("#drpPais").setOnchange(location);
  $("#drpTipoRadicacion").setOnchange(tipoUsuario);
});

function location() {
  var pais = $("#drpPais").getValue();

  if (pais == "CO")
  {
        $("#drpDepartamento").show()
        $("#drpCiudad").show()
  }
  else
  {
	$("#drpDepartamento").hide();
        $("#drpCiudad").hide();        
  }	
}

function tipoUsuario() {
  var usuario = $("#drpTipoRadicacion").getValue();

  if (usuario == "0")
  {
        $("#subtitle0000000001").hide();
        $("#drpTipoEntidad").hide();
        $("#txtNit").hide();
        $("#txtNombreEntidad").hide();
        $("#subtitle0000000002").hide();

        $("#subtitle0000000003").show();
        $("#drpTipoDocumento").show();
        $("#txtNumDoc").show();
        $("#txtNombres").show();
        $("#txtApellidos").show();
        $("#drpPais").show();
        $("#drpDepartamento").show();
        $("#drpCiudad").show();
        $("#txtDireccion").show();
        $("#txtTelefono").show();
        $("#txtCelular").show();
  }
  else if(usuario == "1")
  {
	$("#subtitle0000000001").show();
        $("#drpTipoEntidad").show();
        $("#txtNit").show();
        $("#txtNombreEntidad").show();
        $("#subtitle0000000002").show();

        $("#subtitle0000000003").hide();
  }
  else
  {
	$("#subtitle0000000001").hide();
        $("#drpTipoEntidad").hide();
        $("#txtNit").hide();
        $("#txtNombreEntidad").hide();
        $("#subtitle0000000002").hide();
        $("#subtitle0000000003").hide();
        $("#drpTipoDocumento").hide();
        $("#txtNumDoc").hide();
        $("#txtNombres").hide();
        $("#txtApellidos").hide();
        $("#drpPais").hide();
        $("#drpDepartamento").hide();
        $("#drpCiudad").hide();
        $("#txtDireccion").hide();
        $("#txtTelefono").hide();
        $("#txtCelular").hide();
  }
}
By nobody
#821960
The reason most likely is that the setOnchange event handler is a PM custom event handler that takes two arguments by default - new value and old value.

Read wiki.processmaker.com/3.0/JavaScript_F ... tOnchange for more details.

I would also recommend (if you are in an early stage of development) renaming the IDs of your fields/variables. For example, those fields that should be hidden in scenario 1 could start with a prefix scen1, the others with scen2.

Then, you can use the setOnchange function like this:
Code: Select all
$("#yourControl").setOnchange(function(newVal,oldVal) {
if (newVal==="0") {
$("[id*=scen1]").show();
$("[id*=scen2]").hide();
} else {
$("[id*=scen1]").hide();
$("[id*=scen2]").show();
}}
Hope this helps.
User avatar
By amosbatto
#821997
Your problem is that you have a function named "location", but that conflicts with the location object, so you get an error:
ErrorInJavaScriptWhenLoadingForm.png
ErrorInJavaScriptWhenLoadingForm.png (74.18 KiB) Viewed 4044 times
To see your JavaScript errors, press F12 in Chrome or Firefox to open you browser's debugger and go to the "Console" tab.

If you rename your "location" function to "configureLocation", then it should work:
Code: Select all
configureLocation();
tipoUsuario();
  
$("#drpPais").setOnchange(location);
$("#drpTipoRadicacion").setOnchange(tipoUsuario);


function configureLocation() {
  var pais = $("#drpPais").getValue();

  if (pais == "CO")
  {
        $("#drpDepartamento").show()
        $("#drpCiudad").show()
  }
  else
  {
        $("#drpDepartamento").hide();
        $("#drpCiudad").hide();        
  }	
}

function tipoUsuario() {
  var usuario = $("#drpTipoRadicacion").getValue();

  if (usuario == "0")
  {
        $("#subtitle0000000001").hide();
        $("#drpTipoEntidad").hide();
        $("#txtNit").hide();
        $("#txtNombreEntidad").hide();
        $("#subtitle0000000002").hide();

        $("#subtitle0000000003").show();
        $("#drpTipoDocumento").show();
        $("#txtNumDoc").show();
        $("#txtNombres").show();
        $("#txtApellidos").show();
        $("#drpPais").show();
        $("#drpDepartamento").show();
        $("#drpCiudad").show();
        $("#txtDireccion").show();
        $("#txtTelefono").show();
        $("#txtCelular").show();
  }
  else if(usuario == "1")
  {
	$("#subtitle0000000001").show();
        $("#drpTipoEntidad").show();
        $("#txtNit").show();
        $("#txtNombreEntidad").show();
        $("#subtitle0000000002").show();

        $("#subtitle0000000003").hide();
  }
  else
  {
        $("#subtitle0000000001").hide();
        $("#drpTipoEntidad").hide();
        $("#txtNit").hide();
        $("#txtNombreEntidad").hide();
        $("#subtitle0000000002").hide();
        $("#subtitle0000000003").hide();
        $("#drpTipoDocumento").hide();
        $("#txtNumDoc").hide();
        $("#txtNombres").hide();
        $("#txtApellidos").hide();
        $("#drpPais").hide();
        $("#drpDepartamento").hide();
        $("#drpCiudad").hide();
        $("#txtDireccion").hide();
        $("#txtTelefono").hide();
        $("#txtCelular").hide();
  }
}
Want to create your own meme coin?

In the world of cryptocurrencies, a unique and exc[…]

The market for cryptocurrencies is demonstrating a[…]

What's SAP FICO?

Embarking on a dissertation can be one of the most[…]

Hello. For rental housing, there are software solu[…]