473,386 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Passing arguments dynamically to onclick event / Scope of variables

17
Hi,

Here's my form:

http://www.steinrogan.com/SecureSite_v2_beta/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 2538
Csanád
17
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 Expert Mod 8TB
Try:
Expand|Select|Wrap|Line Numbers
  1. depup.nextSibling.nextSibling.onclick='rmdep('+this+','+i+','+d+'); return false;';
Aug 26 '07 #3
Csanád
17
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_beta/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 2GB
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
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 Expert Mod 8TB
Glad you got it working. So how exactly did you get it working?
Aug 28 '07 #7
Csanád
17
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 2GB
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
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,...
7
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. ...
3
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) {...
2
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...
2
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 } ...
4
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...
7
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...
9
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...
1
by: johnjsforum | last post by:
Buddies, I have a web page to create HTML buttons dynamically as in the “formDivColorPicker” function ////////////////////////////////////////////////////////////////////////////////////...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.