How To Pass Arguments Independently Or Using AddEventListener
Sep 10, 2002
I use addEventListener and I cannot (or at least don't know how) pass arguments to the function.
Let's say we have a function:
function warning(arg1, arg2) {alert('Argument 1: ' + arg1 + ', Argument2: ' + arg2 + '.');}
It's possible to have:
onclick="warning('my argument 1', 'my argument 1');" as an html attribut.
But I think it's not posible to do it like this (still the same function):
el.addEventListener("click", warning('my argument 1', 'my argument 1'), false);
So how can I pass arguments to the function?
View 1 Replies
ADVERTISEMENT
Jan 29, 2006
I am trying to create a script that will cover cross-browser limitations in adding event listeners to elements. I have found plenty of resources to add a listener but can't find any way of assigning a function containing arguments. Here is what I have so far:
/* Attach events regardless of browser */
function addEvent(obj, evType, fn)
{
if (obj.addEventListener)
{
obj.addEventListener(evType, fn, false);
return true;
}
else if (obj.attachEvent)
{
var r = obj.attachEvent("on"+evType, fn);
return r;
}
else
{
return false;
}}
/* Contains all attachments to be made on page load */
function load()
{
item = document.getElementById('toggleControl');
addEvent(item, 'click', toggle);
item = document.getElementById('alertLink');
addEvent(item, 'click', runAlert);
}
/* Add event to page load and assign load() function */
addEvent(window, 'load', load);
The actual functions for toggle and runAlert are in a seperate .js file, but are standard functions. the problem is that I can't find a way to perform the equivalent of:
item = document.getElementById('toggleControl');
addEvent(item, 'click', toggle('toggleID'));
It simply does not work.
View 10 Replies
View Related
Jan 8, 2007
Is there a way to pass arguments to the callback function used inside an addEventListener? I see that I can only list the name of the callback function. For eg, I use this:
var boldLink=document.getElementById('cmtbold');
boldLink.addEventListener("click", rBold, true);
I need the id of boldLink to be accessible from inside the function
rBold()
View 10 Replies
View Related
Jan 9, 2011
I know in different languages (VB.Net,C++,C#) how to pass an argument to function in this code:
<html>
<head>
<script language="Javascript">
<!-- hide
function openNewWindow() {
popupWin = window.open('[URL]',
'open_window',
'menubar, toolbar, location, directories, status, scrollbars, resizable, dependent, width=640, height=480, left=0, top=0')
} done hiding -->
</script></head><body>
<a href="javascript:openNewWindow();">return to front page</a>
</body></html>
I need to add several links in my page, so I need to pass indexes from links to main function, so I can open images in specific folder with indexes filenames. For example in vb
Subname("text_to_pass_to_SubName")
Public Sub SubName(ByVal function_text as string)
End Sub
View 8 Replies
View Related
Jun 19, 2009
I'm use prototype quite a lot for ajax calls, but have always used inline onclicks. I'm trying to be less obtrusive by creating event handlers/listeners.I used to do this with the aid of pHp to place arguments into the function (usually via 'while loop'):
<li><a href="#" onclick="dofunction(arg1,arg2);">item 1</a></li>
<li><a href="#" onclick="dofunction(arg1,arg2);">item 2</a></li>
<li><a href="#" onclick="dofunction(arg1,arg2);">item 3</a></li>
[code]....
View 8 Replies
View Related
Dec 19, 2006
I have a function which can be called with an unlimited number of
arguments.
I want to call another function with exactly the same arguments. I know
I can get the arguments in the arguments object, and as such also in an
array, but how do you pass the elements of an array to another function
as parameters?
View 5 Replies
View Related
Dec 13, 2011
I would like to display few calendar fields on the same page with different days disabled. I guess that the easiest way to do that would be to let thenoWeekendsOrHolidaysfunction take care of it but in order to do that I need to pass some values to it. How can I do that?
$('.datepicker:not(.ui-datepicker)').live('click',function(){
alert(this.name);
//var disabledDays = '12-15-2011';
$(this).datepicker({
[Code].....
View 10 Replies
View Related
May 3, 2011
I haven't come across an exampe that would enable me to pass data to my event handler.For example, if I click a button I want it to pass the value of an adjacent text input to my handler.
View 10 Replies
View Related
Jan 8, 2011
This example is much simplified as the original script.VBScript is making a value, passes this to Javascript, which puts that value into a div tag.Now, as you see in the VBScript, the value contains a call to a javascript function. It shows perfectly in HTML, but the call can't be made when clicking the link.Is it possible in VBscript to write dynamic hardcoded javascipt function that pass arguments?
Code:
<script type="text/vbscript">
Dim m
[code]....
View 6 Replies
View Related
Oct 5, 2010
<html>
<head>
<title>Variable - Examples 1</title>
[code]....
View 1 Replies
View Related
Apr 5, 2009
I'm scripting Adobe InDesign via JS. (don't be frightened this post is purely about JS problem)
I've created custom 'Classes' for custom InDesign objects.They all inherit common methods from generic 'Item' class. Now the problem is that most of these methods need acces to the arguments of the child's constructor.
[Code]..
View 3 Replies
View Related
Nov 7, 2006
I have 10 web links on my index but at the top my page is there one <input type="text" name="myurls"> So I want to create an effect like the following:
<a href="http://www.yahoo.com">Yahoo!</a> <a href="#">click add</a>
When some user press to the "click add" link the url link http://www.yahoo.com is passed on <input type="text" name="myurls"> and if the same user click on other link the present link disappears and the new is writing.
View 1 Replies
View Related
Sep 20, 2010
So I'm working on gallery page. The content is separated by years. I want the years of work to be able to collapse and expand when the year is clicked. I'm not sure how to get each year to act independently of each other and also to have the current year to not be hidden. Also, the way I have it now, it expands out of the screen before correcting itself.
View 1 Replies
View Related
Mar 2, 2011
I have a checkbox inside a div. I need to handle both click eventindependently. When I click on the checkbox, thediv click event is triggered too. Does anyone knows how to manage this?
Here is my code. When clicking on the checkbox, I get 2 alert boxes instead of 1.
jQuery
$(document).ready(function(){
$('div').bind('click' , function(event){
alert('a');
});
$('input').bind('click' , function(event){
[Code]...
View 5 Replies
View Related
Sep 30, 2010
How can I position the game inside the I frame? I can do what ever I want with the iframe, but the content inside the frame won't position independently. I would like to position it absolutely inside the iframe. I just can't find the way. I just so you know this is a game I make in gamemaker and yoyo gave this code to zip and add the exe. I don't have a separate file for the game it appears to be part of the iframe.
View 2 Replies
View Related
Jul 22, 2011
I want to find a way to highlight elements when I click on them. The problem is the elements are nested and when I click on a child element, the parent element is highlighted as well. I would like the child to highlight only, but because the child is within the boundaries of the parent (i.e. you can't hover over the child without also hovering over the parent), the parent highlights as well.
For example, if you have...
<div>
<h1></h1>
<ul>
<li></li>
<li></li>
</ul>
</div>
I'd like to be able to hover over, click and highlight an <li> and hover over, click and highlight the <div> independently of one another. It's no problem to hover over, click and highlight the <div> without highlighting the <li>, but because the <div> encapsulates the <li> it's impossible to hover over, click and highlight the <li> without highlighting the <div>.
View 1 Replies
View Related
Mar 28, 2010
Is there a way I can simplify my code below? I want to be able to have just one iteration of the code and have it automatically switch out the IDs without have to add a new bit of JavaScript with each new div that is added. Here's my Javascript:
$(document).ready(function() {
// toggles the personnel window on clicking the image/header
$('div.title'+'#one').click(function() {
[code]....
View 2 Replies
View Related
Dec 5, 2009
I am new to jquery and need some help with this I have the following simplified html
[Code]...
View 2 Replies
View Related
Mar 7, 2011
I've got a PHP while loop spitting out an article title and it's content from a database and I have links to show or hide the content of the article under each title. I tried simply putting using jquery hide/show on the divs, but as you'd expect this will show and hide every div at the same time. I want to show and hide the content of the article selected as you'd expect.s!
View 9 Replies
View Related
Sep 21, 2005
This makes my life a bit easier. After executing this script you should be able to addEventListener on all elements instead of determining if you want to call attachEvent or addEventListener.
Edit: This is the original version. The revised version is below
if (!document.addEventListener && document.attachEvent)
{
Object.prototype.addEventListener = function(eventName, func, capture)
{
if (this.attachEvent)
this.attachEvent('on' + eventName, func);
}
var i, l = document.all.length;
for (i = 0; i < l; i++)
document.all[i].addEventListener = Object.prototype.addEventListener;
window.addEventListener = Object.prototype.addEventListener;
document.addEventListener = Object.prototype.addEventListener;
}
Revised version:
This one is harder to use but it is nicer to the DOM and all newly created objects. The problem with it is that addEventListener will only be available after the page loads.
If you want to use addEventListener from a window.onload script make sure that this code is included in the body, not in the head. document.body.onload is called before window.onload.
Now, only elements that already have attachEvent will get an addEventListener. Elements created with document.createElement will automatically get addEventListener.
function createIEaddEventListeners()
{
if (document.addEventListener || !document.attachEvent)
return;
function ieAddEventListener(eventName, handler, capture)
{
if (this.attachEvent)
this.attachEvent('on' + eventName, handler);
}
function attachToAll()
{
var i, l = document.all.length;
for (i = 0; i < l; i++)
if (document.all[i].attachEvent)
document.all[i].addEventListener = ieAddEventListener;
}
var originalCreateElement = document.createElement;
document.createElement = function(tagName)
{
var element = originalCreateElement(tagName);
if (element.attachEvent)
element.addEventListener = ieAddEventListener;
return element;
}
window.addEventListener = ieAddEventListener;
document.addEventListener = ieAddEventListener;
var body = document.body;
if (body)
{
if (body.onload)
{
var originalBodyOnload = body.onload;
body.onload = function()
{
attachToAll();
originalBodyOnload();
};
}
else
body.onload = attachToAll;
}
else
window.addEventListener('load', attachToAll);
}
createIEaddEventListeners();
View 7 Replies
View Related
Mar 14, 2009
I'm using the standard module pattern and the problem is once you set a private variable, trying to test that object independently becomes a nightmare as the next test is polluted by the actions of the previous.So, the options are to have some reset method (which is horrible), setters on everything (defeats the point) or delete object and re-load script (hideous).
View 2 Replies
View Related
Nov 16, 2006
Today I have been testing the event models from Netscape 4.8 and IE 4
to the current crop of browsers. I'd like to write a small event
library similar in purpose to the Yahoo! UI event library but with less
features and code. The Yahoo! event library is one of the best
libraries in YUI but it still seems to me to have some confused
code...that or I'm still confused.
The Yahoo! UI library focuses on using addEventListener and
attachEvent. However, due to the click and dblclick bugs in Safari a
long legacy event workaround is included to use a Netscape4-type event
model for Safari. Something like this
var listeners = [function(event){}, function(event){}];
document.getElementById('blue').onmouseover = function(event) {
for (var i=0; i<listeners.length; i++) {
listeners[i](event);
}
};
With this above example, multiple handler functions can be fired for a
single event. I imagine that this is an old trick that has been around
for a long time, yes?
With all the new browsers I tested with this legacy workaround, the
listener handlers can use event.stopPropogation() or
event.cancelBubble=true and they work as desired. The handler functions
can also use event.preventDefault() and event.returnValue=false and
they too work. These seem to work because the event object passed to
the handlers is a modern event object and not one from Netscape4.
My question is, if Safari needs this legacy workaround, and the legacy
workaround seems to work in all the browsers that have addEventListener
or attachEvent, then why bother with the addEventListener and
attachEvent functions at all? Why not just use the legacy way for all
browsers and all type of events.?
View 10 Replies
View Related
Apr 19, 2009
I was using this script to learn how to use event listeners and I need to know how to make it work for IE. I keep finding attachEvent scripts that look like they will work, but I get nothing. I've spent several hours finding script after script that simply don't work. I don't know where to turn next. Any script to attach these events to IE?
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "[URL]">
<html>
<head>
<title>Test</title>
</head>
<body><div>
<script type="text/javascript">
if(!Array.prototype.link)
Array.prototype.link = function(f) { for(var Obect1 = new Array(), i = 0, n = this.length, t = arguments[1]; i < n; i++) Obect1[i] = f.call(t, this[i], i, this);return Obect1;};
Array.prototype.linkMethod = function(m) { var n = this.length, args = this.slice.call(arguments, 1);if(typeof m == "string" && n > 0) m = this[0][m];for(var Obect2 = [], i = 0; i < n; i++) Obect2[i] = m.apply(this[i], args);return Obect2;}; .....
View 2 Replies
View Related
Oct 20, 2006
I'm trying to add a clickevent to an anchor that I created trough DOM.
This his how the code looks:
var oSubLink = document.createElement("A");
oSubLink.appendChild(document.createTextNode("+"));
oCel.appendChild(oSubLink);
oSubLink.addEventListener("click", klapUit(oTabel.id, eigenschappen[2]), false);
It failes at the addEventListener call, saying "No such interface supported" (appears to be one of the two default error messages IE gives when it can't handle your JS :mad: ).
How can I fix this? The solution should work in IE6, FF, Opera, Mozilla and Safari.
View 9 Replies
View Related
Jul 12, 2007
Is there a way to send parameters to the function being added to an event with addEventListener. I.E. say you have this function
Code:
function someFcn(i){
alert(i);
}
and I add it to an object.
Code:
someElement.addEventListener('focus', someFcn, false);
Is there a way to send a parameter to someFcn.
For Example I have tried this but it failed
Code:
var someString = 'Hello World'
someElement.addEventListener('focus', someFcn(someString), false);
View 3 Replies
View Related
Sep 9, 2011
I have this code:
for(var h:Number=0; h<4; h++){
var Build : Button;
Build = new Button();
Build.height = 20;
[Code]...
View 9 Replies
View Related