Two events in function - jQuery

2

I have a function in jQuery which I do through the .on('change') event, but I also needed that same function to happen on page load, but I do not know how to do this, call two events in a function.

Function code in jQuery :

$j("#fullname").on('change',function(){
    alert("Teste");
});
    
asked by anonymous 03.11.2017 / 18:04

2 answers

1

If I understand what you want, you just create a função , so you can run it on page load and click , change or any other way you want to execute.

function funcao(name){
  alert('Meu nome é: ' + name)
}

$('#btn').on('click', function(){
  funcao('Rafael Augusto')
})

funcao('Rafael Augusto')
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><buttonid="btn">Clique aqui</button>
    
03.11.2017 / 18:07
2

Create a trigger with event after page load:

$j(window).on("load", function(){
    $("#fullname").trigger("change");
});

This will trigger the onchange event in the element.

$(window).on("load", function(){
	$("#fullname").trigger("change");
});
$("#fullname").on('change',function(){
    alert("Teste");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><selectid="fullname">
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
</select>
    
03.11.2017 / 18:07