Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Event Handlers

Status
Not open for further replies.

jafisher2000

Programmer
May 5, 2002
25
JM
Could someone tell me how to write event handlers for Dhtml objects such as window.onunload using javascript but not enclosing the actio to be executed within a standalone script tag?
 
Simply put the script statements into the event handler declaration directly, as per:
Code:
<a href=&quot;[URL unfurl="true"]http://www.tek-tips.com&quot;[/URL] onclick=&quot;confirm('Do you really want to leave my site?')?return true; return false;)&quot;>

Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.
 
How does one get a string variable inside an event handler that is declared inside of a string used to set innerHTML?
See code below :

/****************************************/
function addCommentBox( idStr ) {
var el = document.createElement( &quot;div&quot; );
var input = &quot;<INPUT TYPE='BUTTON' onclick='click( this, idStr )' value='Ok'/>&quot;;
el.innerHTML = input;
document.body.appendChild( el );
}

Problem is you need a way to put quotes around the idStr variable which is a parameter to the onclick handler... Nothing I've tried seems to work properly! Thanks for any thoughts or suggestions!
- Delvis
 
Just use the createElement and appendChild functions in the same manner for the button as you do for the div:

Code:
function addCommentBox(idStr){
 var el = document.createElement( &quot;div&quot; );
 var btn = document.createElement( &quot;input&quot; );
 btn.setAttribute(&quot;type&quot;, &quot;button&quot;);
 btn.setAttribute(&quot;onclick&quot;, &quot;doSomething(this, '&quot; + idStr + &quot;')&quot;);
 btn.setAttribute(&quot;value&quot;, &quot;Ok&quot;);
 el.appendChild(btn);
 document.body.appendChild(el);
}

Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.

AU.gif
Check out Tek-Tips Australia New Zealand forum1155
NZ.gif
 

>> Problem is you need a way to put quotes around the idStr variable which is a parameter to the onclick handler

No you wouldn't... Putting quotes around idStr would make it the physical string &quot;idStr&quot;, which you don't want. Unquoted is what you need.

Dan
 
Delvis,

you just need to escape the quotes:

var input = &quot;<INPUT TYPE='BUTTON' onclick='click( this, \&quot;' + idStr + '\&quot; )' value='Ok'/>&quot;;

=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); }
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top