Smudger
Smudger

Reputation: 10771

Javascript on load execution

I have an input field on my PHP page. When a value is entered into the input box a javascript is called and mysql values returned next to the input box. This works 100% without errors.

My code for this is:

<input type=text id=sku1 name=sku1 onchange="showUser(1, this.value)" onkeypress="return enter(document.orderform.sku2)"  value=<? echo $sku1; ?> >

When the user submits the form, the page returns to itself as some calculations are performed. When the page reloads the input box has the value the user entered ($sku1). my problem is that the mysql values are no longer next to the input box as the input box hasnt changed since reloading.

I want to use something like the javaascript OnLoad functionality.

<input type=text id=sku1 name=sku1 onchange="showUser(1, this.value)" OnLoad="showUser(1, this.value)" onkeypress="return enter(document.orderform.sku2)"  value=<? echo $sku1; ?> > 

This however does not work.

Any ideas as how to get the javascript to execute on load as well as when the user changes the value of the input box?

Thanks and Regards, Ryan Smith

Upvotes: 0

Views: 298

Answers (3)

Sarfraz
Sarfraz

Reputation: 382616

You can use onload event like this:

<script>
  var value = document.getElementById('sku1').value;
  window.onload = function() { showUser(1, value); }
</script>

Upvotes: 1

James Glover
James Glover

Reputation: 91

I'd suggest using DOMContentReady, rather than Load, as it will fire earlier. You'll also need to attach the listener to document, rather than the input element itself, as the input element doesn't provide a load event (http://www.w3.org/TR/html4/interact/forms.html#h-17.4)

So, in a script tag, or a separate javascript file:

document.addEventListener('DOMContentReady', function(){
  showUser(1,document.getElementById('sku1'));
})

Upvotes: 1

elrado
elrado

Reputation: 5272

Use jquery's document ready method or try putting showUser in body onload

Upvotes: 2

Related Questions