473,614 Members | 2,377 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

send onclick event to another element

29 New Member
Expand|Select|Wrap|Line Numbers
  1. <SCRIPT LANGUAGE=javascript>
  2.  
  3. function tdclick (ObjTD) {
  4.  
  5.  ArrLinks = ObjTD.getElementsByTagName ("A");
  6.  
  7. document.getElementById("link").onclick();
  8. }
  9.  
  10. </SCRIPT>
  11.  
  12. <table>
  13. <tr>
  14.   <td id="Div_ID" onclick="javascript:tdclick(this);"> <a href="#" id="link" onclick="javascript:alert('It works');">test</a></td>
  15. </tr>
  16. </table>
  17.  
  18.  

Hi

My goal: I have a calendar in table-form. each TD element is a day.
Now they have to click on the hyperlink of the daynumber itself. for example: 3.

I want to change this:
If you click on the TD cell the onclick event of <a href should be executed.

Notice: the <a href is created in javascript as follow:

Expand|Select|Wrap|Line Numbers
  1. Date.toString().link("javascript:SelectDate(" + Date + ");");

So, I don't have the possibility to add an ID to the link.

Note: I don't want to add the onclick event directly in the TD tag. I prefer to make it seperate/dynamic.



If I use the ObjTD.ArrLinks[0].onclick(), it just don't work...
The only way is: document.getele mentbyid...

is there a way around?

I would appreciate your help

thanks in advance
Nov 26 '08 #1
6 15463
tomaz
29 New Member
hm...

I tried:

document.Arrlin ks[0].onclick()
ObjTD.Arrlinks[0].onclick()

ofcourse this don't work...

it's only:

Arrlinks[0].onclick()


sorry guys!
Nov 26 '08 #2
acoder
16,027 Recognized Expert Moderator MVP
That's correct. Two more quick things:
1. Use the script type attribute. The language attribute is deprecated.
2. You don't need the "javascript :" protocol in onclick.
Nov 26 '08 #3
tomaz
29 New Member
thanks acoder, I always receive nice tips from your side.

still, it does not work.

"object doesn't support this property or method"
on following line:
ArrLinks[0].onclick();

Yet the ArrLinks.length > 0
Nov 27 '08 #4
acoder
16,027 Recognized Expert Moderator MVP
Hmm... OK, try this:
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. function tdclick (ObjTD) {
  3.     ArrLinks = ObjTD.getElementsByTagName("a");
  4.     ArrLinks[0].onclick();
  5. }
  6. </script>
  7. <table>
  8. <tr>
  9.   <td id="Div_ID" onclick="tdclick(this);"><br>
  10.     <a href="#" onclick="alert('It works'); return false;">test</a><br>
  11.   </td>
  12. </tr>
  13. </table>
The problem would be that on clicking the link, the event would fire twice. If you want it to be truly dynamic and unobtrusive, then remove the onclick from the link and the table cell and add it dynamically, e.g.
Expand|Select|Wrap|Line Numbers
  1. // reference to table tbl
  2. cellsIn1stRow = tbl.rows[0].cells;
  3. // use a loop to set cells onclick, e.g.
  4. cells[0].onclick = function() {
  5. // do something...
  6. }
Nov 27 '08 #5
tomaz
29 New Member
thanks acoder!! thank you very much.
I've noticed that ArrLinks[0].onclick() doesn't work, but!
ArrLinks[0].click() works fine!
Nov 28 '08 #6
acoder
16,027 Recognized Expert Moderator MVP
click() is not a link method. Which browser(s) did you test? Have you checked that it works in other browsers.
Nov 28 '08 #7

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

Similar topics

2
4431
by: Rob Manger | last post by:
Hi All, I am hoping someone can help me. I am trying to setup my main page so that when the user moves the mouse over an image, it changes the source (got this working). When the user CLICKS on the image (it is a link to the content page) it changes to a third image AND STAYS THERE. ie: so the iamge won't change back to the omouseoff image. Does this make sense? Also, how do I "preload" the images before they are even needed? eg:...
2
18560
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>
17
4869
by: abs | last post by:
My element: <span onclick="alert('test')" id="mySpan">test</span> Let's say that I don't know what is in this span's onclick event. Is it possible to add another action to this element's onclick event ? I've tried something like this: oncl = document.getElementById('mySpan').onclick oncl = oncl + '\n;alert(\'added\')' document.getElementById('mySpan').onclick = oncl
4
2255
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
6400
by: Giacomo | last post by:
I work on a page structured like: <h2> ... </h2> <div ="div1" class="show"> ... </div> <h2> ... </h2> <div id="div2" class="show"> ... </div> <h2> ... </h2> <div id="div3" class="show"> ... </div>
5
2103
by: Fred.Grieco | last post by:
Hi every body, I have a little pb and I'm turning around : function MyFCTN(var1,var2) { var mytable = document.getElementById("myTBL"); for (var i=myTBL.childNodes.length-1; i>0; i--){ myTBL.removeChild(myTBL.childNodes); } for(var i=0; i<var1.length; i++){
11
3001
by: GaryB | last post by:
Hi Guys, I've been battling with this one for hours - I hope that you can help me! My code modifies the <aon a page, from a standard document link into a link with a tailored onclick event. It works perfectly (assigning the correct images and the correct onclick events to the correct <atags):
5
11652
by: GiJeet | last post by:
Hello, I'm trying to figure this code out. <input type="submit" onclick="if(!confirm('Are you sure?') return false; "... /> I know that if you return false in an event like this onclick it prevents the default behavior of the event. So, in the onclick of a button the default behavior is to submit the form. Confirm returns the value 1 if the user clicks OK and the value 0 if
17
2666
by: yawnmoth | last post by:
http://www.frostjedi.com/terra/scripts/demo/this-alert.html http://www.frostjedi.com/terra/scripts/demo/this-alert2.html Why, when you click in the black box, do the alert boxes say different things? Shouldn't they say the same thing?
0
8176
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
8120
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8620
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...
0
8571
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
6085
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4115
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2560
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
1
1705
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1420
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.