team2005
asked on
How to execute javascript on load
Hi!
Have this working javascript, that are called when i select a value from the
combobox -> organisasjon...
But i want this to be called, when the page is loaded to...
How can i do that ?
Have this working javascript, that are called when i select a value from the
combobox -> organisasjon...
<script type="text/javascript">
$(document).ready(function() {
$('#organisasjon').change(function() {
$.ajax({
url : 'selektbrukere.php',
type : 'POST',
dataType: 'json',
data : { organisasjon : $(this).val() },
success : function (data) {
$("#brukere").html(data.text1);
$("#kontrolln load is er").html(data.text2);
}
});
});
});
</script>
But i want this to be called, when the page is loaded to...
How can i do that ?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi!
Okey, that works fine...
Is it possible to select the default value from PHP variable ?
Okey, that works fine...
Is it possible to select the default value from PHP variable ?
ASKER
Hi!
I was mistaken, it dosent works..
When page is loaded, it shows data correct (page loaded)
But when i change organisation combo... it dosent change ?
I was mistaken, it dosent works..
When page is loaded, it shows data correct (page loaded)
But when i change organisation combo... it dosent change ?
Sorry, but are you referring to my answer or to tommyBoy's one?
ASKER
Hi!
You and tommy...
You and tommy...
Try using this slightly modified code by tommyBoy. I used jquery bind() function which should act as standard javascript addEventListener function:
<script>
$(window).on('load', function(){
runAjax('hardcodedValue');
});
$('#organisasjon').bind("change", (function() {
runAjax($(this).val());
}), false);
function runAjax(val) {
$.ajax({
url : 'selektbrukere.php',
type : 'POST',
dataType: 'json',
data : { organisasjon : val },
success : function (data) {
$("#brukere").html(data.text1);
$("#kontrolln load is er").html(data.text2);
}
});
}
</script>
ASKER
Hi!
Default values works fine, but not when organisation is changed ?
Default values works fine, but not when organisation is changed ?
Mmmhh, perhaps we forgot to enclose all code within (document).ready as you did in original snippet:
Secondly, I would add a little alert in the change event:
So we can see if it is raised and the problem resides in another place.
$(document).ready(function() {
});
Secondly, I would add a little alert in the change event:
$('#organisasjon').change(function() {
alert('change event!');
runAjax($(this).val());
});
So we can see if it is raised and the problem resides in another place.
ASKER
Hi!
Still dosent work, dosent alert....
Still dosent work, dosent alert....
<script type="text/javascript">
$(document).ready(function() {
$(window).on('load', function(){
runAjax('Softkey');
});
$('#organisasjon').bind("change", (function() {
alert('change event!');
runAjax($(this).val());
}), false);
function runAjax(val) {
$.ajax({
url : 'selektbrukere.php',
type : 'POST',
dataType: 'json',
data : { organisasjon : val },
success : function (data) {
$("#brukere").html(data.text1);
$("#kontroller").html(data.text2);
}
});
}
});
</script>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Working great now, thanks :)
Open in new window