I want run a function in "onclick" all of my tags that the language of them is English automaticaly (without adding onclick for each tag), like this :
<textbox name="txtTest" lang="en">
is it posible ?
How I can do it ?
| SitePoint Sponsor |



I want run a function in "onclick" all of my tags that the language of them is English automaticaly (without adding onclick for each tag), like this :
<textbox name="txtTest" lang="en">
is it posible ?
How I can do it ?
The following should do what you are after
Code:window.onload = function(){ Language.Init(); //Initialise the language function }; var Language = { Init: function(){ // Get all elements on the page, if you know the elements that you need to search change * to the element type var eles = document.getElementsByTagName('*'); //Loop through all the elements and check if the lang attribute is set to en, if so call Language.Clicked function for(var i=0;i<eles.length;i++){ if(eles[i].getAttribute('lang') == 'en'){ eles[i].onclick = Language.Clicked; }; }; }, Clicked : function(){ //Add click events here } };



thank you very much , and some questions :
1 : can we use custom attributes to our tags ?
2 : can we use document.all for document.getElementsByTagName('*') ?
3 : I always use this syntax for my functions :
But your code have diffrent ,Code:function functionNamr ([parameters]){ [return ([a value]);] }
is it a function or some functions ?
you have two function in a variable and you used it like a DOM : Language.Init();
Can you show me a refrence about this syntax
Hi bakhanbeigi I have answered your questions below, I hope this help but let me know if you need to know anything else.
1 : can we use custom attributes to our tags ?
A: If you want to use custom attributes you will cause your document to no longer validate, depending on what you are wanting to do it may be better to add a class.
2 : can we use document.all for document.getElementsByTagName('*') ?
A: Why do you want to do this all browsers since 1998 support the W3C DOM standard getElementsByTagName.
3 : I always use this syntax for my functions :
A: The format I have used is called object literal it is method I prefer to use as it keeps your code self contained and makes it less likely for the functions to be overwritten by other javascript on the page have a look at this site for more info.



What do the document.getElementsByTagName('*') ?
* is a tag name or means all tags ? what return it ?
* means get all elements on the page it returns an array off the elements it finds, if you know what elements you want it will be better to change this to that elements name as the code will execute quicker. ie document.getElementsByTagName('input');
Bookmarks