Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
|
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 16th, 2005, 11:42 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 217
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to mega
Default Assigning a function to an event

Hi all.

Does anyone have any clue to why this piece of code won't work? It worked for like 2 seconds and then it just stopped working!? I've tried to get it to work for 2 hours in vain. It's supposed to add a function to an elements onclick event.
Code:
/* assign a function to an elements onclick event
by ID and execute the function */
function assignFunctionToId(idName, new_function)
{
  var elm = document.getElementById(idName);
  try {
    elm.onclick = function()
    {
      // evaluate function code
      eval(new_function);
    }
  }
  catch(ex) {
    alert(ex.toString());
  }
}
I also tried with this approach:
Code:
function assignFunctionToId(idName, new_function)
{
  var elms = document.getElementById(idName);
  for (var i = 0; i < elms.length; i++) {
    (function () {
      elms[i].onclick = function () {
    eval(new_function);
      }        
    })();
}
The code I try to add is this:
Code:
/* shows/hides an element by id */
function showhide(myId)
{
  try {
    var state = document.getElementById(myId).style.visibility
    alert(state);
    //alert(document.getElementById(myId) + " state = " + state);
    if (state == 'hidden') {
      document.getElementById(myId).style.visibility = 'visible';
    }
    else {
      document.getElementById(myId).style.visibility = 'hidden';
    }
  }
  catch(ex) {
    alert(ex);
  }
}
The code runs once. I know this because the element correctly hides or show when the page has loaded. The code is assign to window.onload like this:
Code:
window.onload = function()
{
  assignFunctionToId("selLanguage", showhide("languageTOC"));
}
This is my XHTML:
Code:
<h5><a href="#language" id="selLanguage" title="cambio idioma/change language">Cambio idioma<br />Change language</a></h5>
<ul id="languageTOC">
  <li><a href="#" title="">Español</a></li>
  <li><a href="#" title="View the english version of the website">English</a></li>
</ul>
Thanks in advance - I could really use the help!

Regards, Jon.

 - mega
Moving to C# .NET
__________________
- mega
Aspiring JavaScript Ninja
 
Old November 17th, 2005, 07:49 AM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 683
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi Jon,

This should do the trick...
Code:
window.onload = function()
{
  document.getElementById("selLanguage").onclick = new Function("showhide('languageTOC')");
}
HTH,

Chris

 
Old November 21st, 2005, 06:58 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 217
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to mega
Default

Thanks Chris but I found another way:
Code:
/* assign a function to an
 elements event by ID */
function addEvent(oEl, sEvent, sFunction, useCapture) {  
    if (oEl) {
      if (oEl.addEventListener) { // W3C
       oEl.addEventListener(sEvent,sFunction,useCapture);
       return true;
      } else if (oEl.attachEvent) { // Microsoft
       var r = oEl.attachEvent("on"+sEvent,sFunction);
       return r;
      }
    }
}
window.onload = function()
{ 
    var objSelLanguage = document.getElementById("selLanguage");
    if (objSelLanguage) {
        addEvent (objSelLanguage, "click", function() { return showhide("languageTOC") }, false);
    }
}
also I've read that the Function constructor is in many JavaScript implementations less efficient than the
Code:
funtion(x) { return whatever; }
but I think I will use your suggestion in a catch section of addEvent in case addEventListener or attachEvent fails.
 - mega
Moving to C# .NET





Similar Threads
Thread Thread Starter Forum Replies Last Post
calling a codebehind function on onblur event vishnupriya.kamalakaram ASP.NET 1.0 and 1.1 Basics 2 July 7th, 2006 08:25 AM
Assigning Recordsets mrjeret BOOK: Access 2003 VBA Programmer's Reference 0 July 6th, 2006 09:39 AM
How to call a codebehin function on onblur event?? kv Priya ASP.NET 1.0 and 1.1 Professional 1 June 14th, 2006 02:43 PM
calling the user defined function on click event CsharpHelp C# 1 June 6th, 2005 07:25 AM
Assigning radiobuttons hydra14 C# 7 June 5th, 2004 12:25 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.