Wrox Programmer Forums
|
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 September 5th, 2006, 01:00 PM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 139
Thanks: 0
Thanked 0 Times in 0 Posts
Default attachEvent

Hi,

How does one attach an onclick even with arguments?

From the examples that I've been able to find online, they are just straight calls to the function. I want to be able to pass the string 'apple' in the example.. but it doesn't work..

function createTab(text)
{
  var nLink = document.createElement("a");
  nLink.href = "#";
  nLink.attachEvent('onclick',something('apple'));
}

function something(arg)
{
  alert (arg)
}

 
Old September 5th, 2006, 08:26 PM
Val Val is offline
Registered User
 
Join Date: Sep 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

try nLink.onclick=function () { alert ("ok") };

 
Old September 6th, 2006, 12:11 PM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 139
Thanks: 0
Thanked 0 Times in 0 Posts
Default

That won't work, I'm trying to pass an argument to the function... alert the value was just an example, I will be doing other things...

The only way that I have it is by doing this:

document.createElement("<a href=\"#\" onClick=\"something('apple')\" ></a>");

But there has to be a better way of doing this!

 
Old September 7th, 2006, 06:55 PM
Val Val is offline
Registered User
 
Join Date: Sep 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It will work - of course you should replace alert ("ok") on something('apple'). Here the full text (I created a button to rise the event):

<input type=button onclick=createTab("test") value=click> </input>

<script language="JavaScript" type="text/javascript">
function createTab(text)
{
  var nLink = document.createElement("a");
  nLink.href = "#";
  var oText = document.createTextNode(text);
// nLink.attachEvent('onclick',something('apple'));
  nLink.onclick=function () { something('apple') };
  nLink.appendChild(oText);
  document.body.appendChild(nLink);
}

function something(arg)
{
  alert (arg)
}

</script>






Similar Threads
Thread Thread Starter Forum Replies Last Post
attachEvent !? nerssi Javascript 0 April 7th, 2008 09:54 AM
about attachEvent mothod of document or window? nerssi Javascript 2 September 16th, 2004 11:14 AM





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