SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Hybrid View

  1. #1
    SitePoint Evangelist
    Join Date
    Dec 2005
    Posts
    527
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    some setting for a tag witg special parameters

    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 ?

  2. #2
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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	
    		}
    	};

  3. #3
    SitePoint Evangelist
    Join Date
    Dec 2005
    Posts
    527
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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 :

    Code:
       function functionNamr ([parameters]){
        [return ([a value]);]
       }
    But your code have diffrent ,
    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

  4. #4
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  5. #5
    SitePoint Evangelist
    Join Date
    Dec 2005
    Posts
    527
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What do the document.getElementsByTagName('*') ?

    * is a tag name or means all tags ? what return it ?

  6. #6
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    * 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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •