473,783 Members | 2,564 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Passing arguments dynamically to onclick event / Scope of variables

17 New Member
Hi,

Here's my form:

http://www.steinrogan. com/SecureSite_v2_b eta/addthis.php

When I add two or more Dependant Items and remove one that's not the last that I added, I won't be able to remove any more Dependant Items. Say I deleted the first Dependant Item and try to remove the second Dependant Item - it will try to look for the link inactive.html that should never be accessed.

I believe the problem is with reassigning the onclick attribue values. I spent the whole day yesterday on this, but can't figure out what I'm doing wrong.

Any help is greatly appreciated,

Thank you!

Csanád
Aug 24 '07 #1
8 2565
Csanád
17 New Member
Hi,

I have the following function. Everything works except the assignment of the onclick event on line 20. I need to pass 3 arguments dynamically to the function call like this: rmdep(this, i, d). How do I go about this?

Thanks for your time,

Csanád

Expand|Select|Wrap|Line Numbers
  1. function rmdep(what,i,d)
  2. { // i is the index of current Item
  3.   // d is the index of current Dependant Item
  4.   var deps=document.getElementById('dep_'+i).value; // deps is the number of Dependant Items for current Item
  5.   if (d==deps) // remove the last Dependant Item
  6.   {
  7.     deps--;
  8.     document.getElementById('dep_'+i).value=deps;
  9.   }
  10.   else // remove Dependant Item from the middle of list; renumber following Dependant Items 
  11.   {    
  12.     var q=d+1;
  13.     var depup;
  14.     for (q; q<=deps; q++)
  15.     {
  16.         depup=document.getElementById(i+'_'+q);
  17.         depup.setAttribute('name','f'+i+'_'+d);
  18.         depup.setAttribute('id',i+'_'+d);
  19.         depup.previousSibling.previousSibling.firstChild.nodeValue='Dependant Item ['+d+']';
  20.         depup.nextSibling.nextSibling.onclick='rmdep(this,i,d); return false';
  21.         depup.nextSibling.nextSibling.setAttribute('id','r_'+i+'_'+d);
  22.         d++;
  23.     }
  24.     deps--;
  25.     document.getElementById('dep_'+i).value=deps; 
  26.   }    
  27.   while (what && what.tagName!='DIV') { what=what.parentNode; }
  28.   if (what) what.parentNode.removeChild(what);
  29. }
Aug 25 '07 #2
acoder
16,027 Recognized Expert Moderator MVP
Try:
Expand|Select|Wrap|Line Numbers
  1. depup.nextSibling.nextSibling.onclick='rmdep('+this+','+i+','+d+'); return false;';
Aug 26 '07 #3
Csanád
17 New Member
Thanks Acoder! Your help is greatly appreciated, but something is still not quite right with my code and it's driving me crazy! :)

You can check it out here:

http://www.steinrogan. com/SecureSite_v2_b eta/addthis.php

Please do the following:

- add 4 Dependant Items,
- remove the second Dependant Item (the idea here is that Dependant Items [3] and [4] get renumbered to [2] and [3], respectively)
- try removing the very first Dependant Item.

This is where the code crashes. I believe the trouble is coming from the "this" keyword's reassignment, but I'm not sure.

Any ideas?

Thanks very much!!

Csanád
Aug 27 '07 #4
dmjpro
2,476 Top Contributor
Try:
Expand|Select|Wrap|Line Numbers
  1. depup.nextSibling.nextSibling.onclick='rmdep('+this+','+i+','+d+'); return false;';
It will work fine if you write.......

Expand|Select|Wrap|Line Numbers
  1. depup.nextSibling.nextSibling.setAttribute('onclick','rmdep(,'+i+','+d+'); return false;');
this needs not to be appended, but you have to append i and d these are the JavaScript Variables.
I hope it will work fine.
And all the best with your try.

Kind regards,
Dmjpro.
Aug 28 '07 #5
Csanád
17 New Member
The problem is gone. :) Thank you so much for your help Acoder and Dmjpro!!

All the best!

Cheers,

Csanád
Aug 28 '07 #6
acoder
16,027 Recognized Expert Moderator MVP
Glad you got it working. So how exactly did you get it working?
Aug 28 '07 #7
Csanád
17 New Member
Glad you got it working. So how exactly did you get it working?
Truth be told, I don't know. :) All I did was that I deleted all the alerts from the function that were there to help me find the bug...and shushhh, with that, everything worked like a charm. In other words, the error I described earlier could not be reproduced. It's still a mystery. But it's working, so I'm happy!

Thank you again for your help!

Cheers,

Csanád
Aug 28 '07 #8
dmjpro
2,476 Top Contributor
It will work fine if you write.......

Expand|Select|Wrap|Line Numbers
  1. depup.nextSibling.nextSibling.setAttribute('onclick','rmdep(,'+i+','+d+'); return false;');
this needs not to be appended, but you have to append i and d these are the JavaScript Variables.
I hope it will work fine.
And all the best with your try.

Kind regards,
Dmjpro.
I did a mistake here.....!!

Expand|Select|Wrap|Line Numbers
  1. depup.nextSibling.nextSibling.setAttribute('onclick','rmdep(this,'+i+','+d+'); return false;');
Kind regards,
Dmjpro.
Aug 29 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

10
63611
by: Free-Ed, Ltd. | last post by:
I am going nuts trying to find a paragraph in a book that described how to change the text content (HTML) in a DIV. Actually I have an array of HTML strings that I want to drop into the DIV, depending on button clicks, etc. Putting this stuff into TEXTAREA and TEXT is simple, but I want to be able to mix font families, weights, sizes, and stuff like that. Thanks for your help.
7
49582
by: Pavils Jurjans | last post by:
Hallo, I have been programming for restricted environments where Internet Explorer is a standard, so I haven't stumbled upon this problem until now, when I need to write a DOM-compatible code. The question is about best practices for passing parameters to an event function. I have, say, the following HTML:
3
14952
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) { document.images.src = eval("mt" +menu+ ".src") } alert("imgOff_hidemenu"); hideMenu=setTimeout('Hide(menu,num)',500);
2
1997
by: Adi | last post by:
Okay, this issue has been annoying me for a little while now. I need it to work on Mozilla 1.6 & IE 6 This is what I would like to be able to do: var row = document.createElement("TR"); var tdImage = document.createElement("TD"); var img = document.createElement("IMG"); img.src = "results.gif"; var theA = document.createElement("A");
2
18581
by: RobG | last post by:
I am trying to dynamically add an onclick to an element, however I just can't get the syntax right. consider the following function: function doClick (evt,x) { // do things with evt and x } Which is called statically by: <button onclick="doClick(event,this);">Click me</button>
4
2270
by: RobG | last post by:
I have a function whose parameter is a reference the element that called it: function someFunction(el) { ... } The function is assigned to the onclick event of some elements in the HTML source:
7
1984
by: Bonzo | last post by:
>From within a function, I want to pass a/some parameters to another function, AND all arguments, passed into this function. e.g. function firstFunction(){ //this function may have been passed, 0, 1, or n arguments... ... //call another function... var someCalculatedValue = 'someValue';
9
3345
by: zholthran | last post by:
Hi folks, after reading several threads on this issue (-> subject) I fear that I got a problem that cannot easily be solved by the offered workarounds in an acceptable way, at least not with my limited c & c++ experience. Maybe some of you can help. the problem: I need several instances of a class whose (non-static!) methods should serve as callbacks for a dll (which can' be manipulated/adapted in any
1
2284
by: johnjsforum | last post by:
Buddies, I have a web page to create HTML buttons dynamically as in the “formDivColorPicker” function //////////////////////////////////////////////////////////////////////////////////// version 1 : using global variables ////////////////////////////////////////////////////////////////// var _textField, _divColorPicker; // global vars window.onload = function() { _textField = document.getElementById('htxaMessage'); // fill...
0
9643
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10315
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10083
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9946
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8968
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5379
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4044
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.