Re: [LOULUG] Javascript Object referencing

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Re: [LOULUG] Javascript Object referencing

Errol Sayre
I found an answer... from the Apple Dashboard Developer's list...  
another reason for me to be using the Prototype framework... it  
includes the handy bind function, which uses the built-in "apply"  
function to bind DOM elements to code much like Cocoa bindings...

This is another case of "wish I could use the right terminology to  
find a hit in google".

I've attached my updated sample code if anyone wants to take a look.  
This is probably the simplest example I could make

Errol

test_self_referencing.html (2K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: [LOULUG] Javascript Object referencing

Eli Friedman
I think your method is overkill; all you need to do is declare the
function and set the onclick handler to use it.  I attached how I would
do it.

-Eli

Errol Sayre wrote:

> I found an answer... from the Apple Dashboard Developer's list...  
> another reason for me to be using the Prototype framework... it  
> includes the handy bind function, which uses the built-in "apply"  
> function to bind DOM elements to code much like Cocoa bindings...
>
> This is another case of "wish I could use the right terminology to  find
> a hit in google".
>
> I've attached my updated sample code if anyone wants to take a look.  
> This is probably the simplest example I could make
>
> Errol
>
> ------------------------------------------------------------------------
>

Test OOP Self Referencing <script type="text/javascript" language="javascript1.5"> // <![CDATA[ // set up our buttons var myButton1 = null; var myButton2 = null; var myButton3 = null; // Set up our class that uses the bind method function MessageButton(theDiv, theSuppliedLabel, theSuppliedMessage) { function showMessage() { // display the alert alert(theButton.value + "\n" + theMessage); } // this class sets up a button inside the supplied div that // displays a message (as supplied by the caller) when clicked var theMessage = theSuppliedMessage; // create the button var theButton = document.createElement("input"); theButton.type = "button"; theButton.value = theSuppliedLabel; // add the onclick behavior // try to bind the local showMessage method to the button theButton.onclick = showMessage; // add the button to the div theDiv.appendChild(theButton); } // since it's more certain than using a defer attribute in the // script tag, setup an onload handler on the div to call this // function function setupButtons() { // create a couple buttons myButton1 = new MessageButton(document.getElementById("theActionBay"), "One", "Your mom goes to college."); myButton2 = new MessageButton(document.getElementById("theActionBay"), "Two", "Do the chicken's have large talons?"); myButton3 = new MessageButton(document.getElementById("theActionBay"), "Three", "With shotguns, what else?!"); } // ]]> </script>