Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Jan 2004
    Posts
    185
    Thanks
    2
    Thanked 1 Time in 1 Post

    trying to enumerate document-and-down by tag? class? anything?

    hi. something like this works, or worked, at some point

    Code:
    function init()
    {
    var p=document.getElementsByTagName("input");
    for (var i=0;i<p.length;i++){
    p[i].onfocus = p[i].style.borderColor="black";
    p[i].onblur =  p[i].style.borderColor="white";
    }
    }
    or is for..in meant to be used in these situations? i dunno i just want to be able to apply some behaviours to all of "x" whether that's all the children of a particular parent or all instances of a tag or class, that'll give me a jumping off point.

  • #2
    Regular Coder
    Join Date
    Aug 2004
    Location
    codegoboom@yahoo.com
    Posts
    999
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    function init()
    {
    	var p = document.getElementsByTagName("input");
    	var i = p.length;
    	while(i--)
    	{
    		p[i].onfocus = function(){this.style.borderColor="black"};
    		p[i].onblur =  function(){this.style.borderColor="white"};
    	}
    }
    That's one way.
    *this message will self destruct in n-seconds*

  • #3
    Regular Coder
    Join Date
    Jul 2002
    Posts
    698
    Thanks
    0
    Thanked 0 Times in 0 Posts
    same as codge just written differently

    function init()
    {
    var p=document.getElementsByTagName("input");
    for (var i=0;i<p.length;i++){
    p[i].onfocus =new Function("this.style.borderColor='blue';");
    p[i].onblur =new Function("this.style.borderColor='white';");
    }
    }

  • #4
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Or, more efficiently:
    Code:
    function init(){
        var
            i=0,
            coll=document.getElementsByTagName('input'),
            elm;
        while(elm=coll.item(i++)){
            elm.onfocus=function(){this.style.borderColor='blue';};
            elm.onblur=function(){this.style.borderColor='white';};
        }
    }
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #5
    Regular Coder
    Join Date
    Jan 2004
    Posts
    185
    Thanks
    2
    Thanked 1 Time in 1 Post
    thanks.

    as an aside: if anyone knows the semantics of for..in i'd be curious. i vaguely remember something like

    Code:
    var p = document.getElementById('somelayer');
    for (i in p) {
    someotherlayer.innerText += p[i];
    }

  • #6
    Regular Coder
    Join Date
    Dec 2002
    Posts
    169
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Here's a handy bookmarklet that uses for..in:

    Quote Originally Posted by subhailc
    thanks.

    as an aside: if anyone knows the semantics of for..in i'd be curious. i vaguely remember something like

    Code:
    var p = document.getElementById('somelayer');
    for (i in p) {
    someotherlayer.innerText += p[i];
    }
    In your browser's address bar, type:

    Code:
    javascript:
    (function(objName){obj=eval(objName);var r=[];for(var i in obj){r[r.length]=objName+'.'+i+' = '+obj[i];if(obj==obj[i])r[r.length-1]+=' ****';if(obj[i]==null)r[r.length-1]='\t'+r[r.length-1]}r.sort();alert(r.join('\n')+'\n\n**** refers to '+objName);})(prompt('Object:','this'));
    void(0);/*property: Copyright (c) 2003-2004, by Rick Renfrow.*/
    Supply the name of a DOM object and press enter. An alphabetized list of the object's properties is displayed.

    Does this help?


  •  

    Posting Permissions

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