SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2006
    Location
    Leeds, UK
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question function checkNode() help (working: code up, thanks!)

    Hi all,
    This is my first post, I'd really appreciate your help on this. I've tried to provide as much info as I can on what I'm trying to do. The function described below only returns the correct result if all arguments are provided, it needs to be able to take combinations of the last 3 arguments.

    I really hope a) it makes sense!! and b) someone could help, I'd be very grateful.

    Thanks a lot,

    Jamie

    k, here goes!

    checkNode() takes a reference to a node received elsewhere by window.event.srcElement or e.target.

    What I want it to do is be able to check all possible combinations of;

    1. Node Name
    2. Attribute Name
    3. Attribute Name with Value.

    Say for example a user clicks a submit button, causing my script to be passed a reference to the <input /> node via window.event.srcElement or e.target. I want to be able to call checkNode(el,'input','type','submit') and depending on the match, get true or false back. If it's true I might call a form validating script for example.

    In other cases I might want to check things like;

    checkNode(el,'input'); // is el an <input /> ?
    checkNode(el,'span','title'); // is el a <span /> with a title attribute of any value?
    checkNode(el,'span','title','hello world'); // is el a <span /> with a title attribute with a value of "hello world"?
    checkNode(el,'','class'); // is el any node with a class of any value? (probably useless, but you get the idea)
    checkNode(el,'','class','someclass'); // is el any node with a class with a value of "someclass"?

    Function Arguments
    el = variable reference to node (required), received by window.event.srcElement or e.target.
    n = nodeName Sought (optional).
    a = Attribute Sought (Optional, required when searching variable value).
    v = Attribute Value Sought (optional).

    Commented Code Below;
    Code:
    /* Compressed */
    function checkNode(el,n,a,v) {
    var ok=true;
    ok=n&&ok?el.nodeName.toLowerCase()==n?true:false:true;
    ok=a&&ok?el.getAttribute(a)?true:false:false;
    ok=a&&v&&ok?el.getAttribute(a)&&el.getAttribute(a)==v?true:false:true;
    return ok;
    }
    
    /* Readable */
    function checkNodeBloat(el,n,a,v) {
    var ok = true; // this will stay true until a sought match is false
    
    // NODENAME
    if(n&&ok) { // if we want to check the nodeName
    	if(el.nodeName.toLowerCase()==n){ // then check the nodeName matches
    		ok = true; // it does, keep ok true in order to look for attributes if applicable
    	}
    	else{
    		ok = false; // any checks for attribute or value won't be made and function will return false
    	}
    }
    else {
    	ok = true; // To get here it means we're not looking for a nodeName, so just continue.
    }
    // ATTRIBUTE
    if(a&&ok) { // if we want to check the node has a named attribute
    	if(el.getAttribute(a)){ // then check
    		ok = true; // it does, keep ok true in order to look for attribute values if applicable
    	}
    	else{
    		ok = false; // any checks for attribute value won't be made and function will return false
    	}
    }
    else {
    	ok = true; // To get here it means we're not looking for an attribute, so just continue.
    }
    // ATTRIBUTE VALUE
    if(a&&v&&ok) { // if we want to check the node has a named attribute of a specific value
    	if(el.getAttribute(a)&&el.getAttribute(a)==v){ // then check
    		ok = true; // it does, keep ok true to return true
    	}
    	else{
    		ok = false; // function will return false
    	}
    }
    else {
    	ok = true; // To get here it means we're not looking for an attribute value, so just continue.
    }
    
    return ok; // true/false
    }
    Last edited by NakedIntruder; Jul 17, 2006 at 02:39.

  2. #2
    SitePoint Wizard chris_fuel's Avatar
    Join Date
    May 2006
    Location
    Ventura, CA
    Posts
    2,750
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    It think something is wrong with your terciary operator (the ? : deal). The format is:

    ( condition ) ? if true : if false;

    What you have above seems to be vastly different.

  3. #3
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Looks ok.

    You can check the arguments actually passed to a function using the arguments object, and take the appropriate action if one or more is missing.

    Here's a sample function I stole from the windows script documentation
    Code:
    function ArgTest(a, b){
       var i, s = "The ArgTest function expected ";
       var numargs = arguments.length;     //Get number of arguments passed.
       var expargs = ArgTest.length;       //Get number of arguments expected.
       if (expargs < 2)
          s += expargs + " argument. ";
       else
          s += expargs + " arguments. ";
       if (numargs < 2)
          s += numargs + " was passed.";
       else
          s += numargs + " were passed.";
       s += "\n\n"
       for (i =0 ; i < numargs; i++){      //Get argument contents.
       s += "  Arg " + i + " = " + arguments[i] + "\n";
       }
       return(s);                          //Return list of arguments.
    }
    Hope that helps,

    Jim

  4. #4
    SitePoint Enthusiast
    Join Date
    Jul 2006
    Location
    Leeds, UK
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by chris_fuel
    Hi,

    It think something is wrong with your terciary operator (the ? : deal). The format is:

    ( condition ) ? if true : if false;

    What you have above seems to be vastly different.
    Hi Chris,
    Thanks a lot, if my understanding of ternary operators is right, the ternary line below...

    ok=n&&ok?el.nodeName.toLowerCase()==n?true:false:true;

    should (hopefully) be equal to;
    Code:
    if(n&&ok){
    	if(el.nodeName.toLowerCase()==n){
    		ok=true
    	}
    	else{
    		ok=false
    	}
    }
    else{
    	ok=true
    }
    Is my Ternary approach doing the same as my if/else above? Thanks again for your help.

    Quote Originally Posted by jimfraser
    Looks ok.
    You can check the arguments actually passed to a function using the arguments object, and take the appropriate action if one or more is missing.

    Here's a sample function I stole from the windows script documentation
    Code:
    function ArgTest(a, b){
       var i, s = "The ArgTest function expected ";
       var numargs = arguments.length;     //Get number of arguments passed.
       var expargs = ArgTest.length;       //Get number of arguments expected.
       if (expargs < 2)
          s += expargs + " argument. ";
       else
          s += expargs + " arguments. ";
       if (numargs < 2)
          s += numargs + " was passed.";
       else
          s += numargs + " were passed.";
       s += "\n\n"
       for (i =0 ; i < numargs; i++){      //Get argument contents.
       s += "  Arg " + i + " = " + arguments[i] + "\n";
       }
       return(s);                          //Return list of arguments.
    }
    Hope that helps,

    Jim
    Fantastic, I'd not heard of that, I'll have a look into it and see what I can do with it, thanks again Jim.

  5. #5
    SitePoint Enthusiast
    Join Date
    Jul 2006
    Location
    Leeds, UK
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a lot for your help, I think I've done it. Here's the code if anyone wants it.

    Code:
    function checkNode(el,n,a,v) {
    var ok=true;
    nok=n&&ok?el.nodeName.toLowerCase()==n?true:false:true;
    aok=a&&ok?el.getAttribute(a)?true:false:true;
    vok=a&&v&&ok?el.getAttribute(a)&&el.getAttribute(a)==v?true:false:true;
    return nok==false||aok==false||vok==false?false:true;
    }
    The code I used to test this was;
    Code:
    var myAll = 'is p '+ checkNode(el,'p','','') + '\n';
    myAll += 'is p class '+ checkNode(el,'p','class','') + '\n';
    myAll += 'is p class="centre" '+ checkNode(el,'p','class','centre') + '\n';
    myAll += 'is class="centre" '+ checkNode(el,'','class','centre') + '\n';
    myAll += 'has class '+ checkNode(el,'','class','');
    alert( myAll );
    As before, el is a reference to a node however you get it, getElementsByTagName, GetElementById, e.target, window.event.srcElement etc.


    Thanks again for your help, much appreciated.

    Jamie


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
  •