Web Analytics Made Easy -
StatCounter getting custom parent tag? - CodingForum

Announcement

Collapse
No announcement yet.

getting custom parent tag?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • getting custom parent tag?

    Hey,

    I need to be able to return true or false in a function to determine if the users cursor is inside a custom tag field.

    eg: the HTML code would be

    <matt>Cutsom Tag</matt>

    and I am trying to use the following code which is called on a keypress....

    Code:
    function isMatt()
    {
    
    	iView.focus();
    	
    	var cursor = iView.document.selection.createRange();
    	var element = cursor.parentElement();
    
    		if (element.nodeName.toLowerCase() == "matt") {
    			//a form has been found
    			return true;
    		} else
    		{
    		
    			while (element.nodeName.toLowerCase() != "matt") {
    				//	if there's a parentNode available, work upon that
    				if (element.parentNode) {
    					element = element.parentNode;
    				} 
    				
    				if (element.nodeName.toLowerCase() == "matt") {
    					//a form has been found
    					return true;
    					break;
    				}
    				
    				if (element.nodeName.toLowerCase() == "html") {
    					break;
    				}
    				
    			}
    		}
    	  
    }
    so basically in the iView (iFrame) when the key is pressed it calls this function... it works perfectly with proper elements, such as <form> or <P> but not with a custom html field.

    Is this the correct way to do it? or is there another way??

    thanks for any help.
    Last edited by homerUK; Mar 12, 2004, 09:59 AM.
    www.mattfacer.com

  • #2
    might it be possible to use the getelementbyid("<matt>") or something??

    thanks..
    www.mattfacer.com

    Comment


    • #3
      hmm..I've tried all sorts to solve this one... still cant seem to be able to get it to work.

      How about if I wrote a custom function that checks the character either side of the cursor position using an onkey/onclick event.

      If it is a "<" or ">" then carry on moving and reading the chars until you reach another "<" or ">" - then compare the letters which you've just read in. If they equal the custom tag for example "MATT" then stop the user from deleting it....

      would that be possible?? thanks...
      www.mattfacer.com

      Comment


      • #4
        ..........anyone?!

        is it possible to use XML to create the custom tag? then find that?
        www.mattfacer.com

        Comment


        • #5
          how about isInsideMatt ?

          if false, do nothing, otherwise doSomethingSignificant();

          I've not used createRange yet. I


          PHP Code:
          function isInsideMatt() {
              var 
          selection iView.document.selection.createRange();
              return (
          findAncestorWithTag(selection.startContainer"matt");

          PHP Code:
          findAncestorWithTag(eltagName) {
              if(
          el == null)
                  return 
          null;
              for(var 
          el.parentNode;!= null;){
              
                  if( 
          p.tagName == tagName )
                      return 
          true;
                      
                  
          p.parentNode;
              }
              return 
          false;

          Last edited by DHTML Kitchen; Mar 16, 2004, 07:52 AM.
          http://dhtmlkitchen.com/

          Comment


          • #6
            thanks for the reply... I've tried your code.. but "el" doesnt seem to be populating. I put some alerts in there to try and determine whats up... but it always has "el" as null....

            Code:
            function getKeyPress()
            {
            	iView.focus();
            	if (isInsideMatt())
            	{
            		alert("we got one!");
            	}  
            }
            
            function isInsideMatt()
            { 
                var selection = iView.document.selection.createRange(); 
                return (findAncestorWithTag(selection.startContainer, "matt"));
            }
            
            function findAncestorWithTag(el, tagName)
            { 
            	if(el == null) 
            		alert("nothing found");
                    return null; 
                for(var p = el.parentNode;p != null;)
            	{ 
                 
                    alert(p.tagName);
            		
            		if( p.tagName == tagName ) 
                        return true; 
                         
                    p = p.parentNode; 
                } 
                return false; 
            }
            www.mattfacer.com

            Comment


            • #7
              Nothing wrong with your code. Much wrong with how IE works. Without declaring an HTML namespace and prefixing your custom elements, IE will do some funky stuff in the DOM.

              <matt>Hello</matt>

              You would expect to be parsed as HTMLUnknownElement with a single child being a text node. However, in IE it is parsed as 3 adjacent nodes! The first is an HTMLUnknownElement with nodeName "matt", the second is the text node, the last is another HTMLUnknownElement with nodeName "/matt". Really, really dumb.

              Anyway, two solutions:

              <html xmlns:name>
              blablabla
              blablabla
              <name:matt>Hello</name:matt>

              Now it will be parsed into the DOM correctly, with NAME:MATT as the nodeName. Alternatively, you could just leave it as it, and look at the previousSibling of the textNode selection to see if it is MATT.
              jasonkarldavis.com

              Comment


              • #8
                cheers for all the help guys!!

                I did have a quick go at the XML approach - but realised that it may cause more problems in the future.

                I ended up coding an onkeypress event which reads the characters before and after the cursor position.

                They are continued to be read until they reach a { which denotes a field marker (eg: <MATT>)

                Its a long way around - but I can do more with the code this way.

                Thanks again for the ideas and support
                www.mattfacer.com

                Comment

                Working...
                X