tags:

views:

43

answers:

4

Is it possible to trigger a Javascript script, when an input element or any other html element is rendered. This script should be triggered from within the html tag, so that we should be able to pass 'this' to the js function.

+2  A: 

No, there is no such event.

However, a <script> tag placed directly after the HTML element would have a similar effect: It would be executed directly after the element has been rendered:

<input type="text" id="input123" value="Hello World!">

<script>
alert("Input123 is now ready:"+document.getElementById("input123).value);
</script>

In most cases, however, it is best to use the document-wide load (or DOMReady, or jQuery's .ready()) to start any script operations. The DOM will be fully ready then.

Pekka
A: 

No this is not possible. You could however use jQuery + $(document).ready(function) to modify any input field you want right after the page has finished loading.

halfdan
+1  A: 

No. However, any script placed after the markup of the input element, will be run with the input element available because it is parsed before the script. So, if this was all in the body:

<input id="a">
<script>document.getElementById('a');</script>

That would all work anyway.

Delan Azabani
the problem is the id is not known to me when the element is rendered.
imdad
How would you know which input to run the code on?
Gareth
Gareth, notice how I selected the element with id `a` in the example script. imdad, you can use other means of selecting the element, such as tag selection, or even class selection.
Delan Azabani
A: 

No, there is not. Just run your code from the document "ready" handler, or use something like jQuery to give you a way to run code at "ready" (which is when the DOM is complete but images may not have loaded).

Pointy