473,386 Members | 1,644 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.

send onclick event to another element

29
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.getelementbyid...

is there a way around?

I would appreciate your help

thanks in advance
Nov 26 '08 #1
6 15452
tomaz
29
hm...

I tried:

document.Arrlinks[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 Expert Mod 8TB
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
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 Expert Mod 8TB
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
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 Expert Mod 8TB
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
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...
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 } ...
17
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...
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: 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">...
5
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--){...
11
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. ...
5
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...
17
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...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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,...
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.