Page 1 of 2 12 LastLast
Results 1 to 10 of 18

Thread: "document.getElementById" issue.

  1. #1
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question "document.getElementById" issue.

    Hello everyone, I just came to know about a document.getElementById issue. What the problem is document.getElementById only gets the first element which has a certain id. What I mean is if I have this piece of code:

    Code:
    <html>
    <head>
    <title></title>
    <script type="text/javascript">
    function disappear(){
    var testdiv = document.getElementById("test");
    testdiv.style.visibility = "hidden";
    }
    </script>
    </head>
    <body>
    <div id="test" style="width: 100px;height: 100px;background-color: red"></div>
    <div id="test" style="width: 100px;height: 100px;background-color: red"></div><div id="test" style="width: 100px;height: 100px;background-color: red"></div><div id="test" style="width: 100px;height: 100px;background-color: red"></div><div id="test" style="width: 100px;height: 100px;background-color: red"></div><div id="test" style="width: 100px;height: 100px;background-color: red"></div>
    <input type="button" value="Disappear" onclick="disappear();">
    </body>
    </html>
    And if I click on the button only the first(the div with test id which appears at first) disappears and not the others . Now I have no idea how to make all the divs disappear. Any help would be appreciated.
    Thanks.

  2. #2
    Join Date
    Sep 2005
    Posts
    882
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default

    According to the W3 specs only one element may have a certain ID.
    That is the difference between classes and IDs. This is the way it is intended to work.

  3. #3
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    blm126: yes I know but in my case I can't loop through arrays having the same class name.

  4. #4
    Join Date
    Sep 2005
    Posts
    882
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default

    Why Not?
    Code:
    function hideclass(searchClass) {
    	var els = document.getElementsByTagName("*");
    	var elsLen = els.length;
    	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
    	for (i = 0; i < elsLen; i++) {
    		if ( pattern.test(els[i].className) ) {
                            els[i].style.visibility = "hidden";
    		}
    	}
    }

  5. #5
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Because I want variables to be passed to the element.

    Code:
    var obj = getElementsById("drag");
    for(var i = 0;i < obj.length;i++){
    objX = parseInt(obj[i].offsetLeft);
    objY = parseInt(obj[i].offsetTop);
    objH = parseInt(obj[i].offsetHeight);
    objW = parseInt(obj[i].offsetWidth);
    objXpW = objX + objW;
    objYpH = objY + objH;
    }
    
    if(objX > targX && objY > targY && objX < targXpW && objY < targYpH){
    next.innerHTML = "Got It!!!";
    } else {
    next.innerHTML = "";
    }
    
     function getElementsById(sId)
     {
        var outArray = new Array();	
    	if(typeof(sId)!='string' || !sId)
    	{
    		return outArray;
    	};
    	
    	if(document.evaluate)
    	{
    		var xpathString = "//*[@id='" + sId.toString() + "']"
    		var xpathResult = document.evaluate(xpathString, document, null, 0, null);
    		while ((outArray[outArray.length] = xpathResult.iterateNext())) { }
    		outArray.pop();
    	}
    	else if(document.all)
    	{
    		
    		for(var i=0,j=document.all[sId].length;i<j;i+=1){
    		outArray[i] =  document.all[sId][i];}
    		
    	}else if(document.getElementsByTagName)
    	{
    	
    		var aEl = document.getElementsByTagName( '*' );	
    		for(var i=0,j=aEl.length;i<j;i+=1){
    		
    			if(aEl[i].id == sId )
    			{
    				outArray.push(aEl[i]);
    			};
    		};	
    		
    	};
    	
    	return outArray;
     }

  6. #6
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by shachi
    [Why isn't using class names to identify multiple elements] Because I want variables to be passed to the element.
    That's not an answer that means anything; variables can't be passed to an element as elements aren't functions.

    There's nothing in the code that you posted that suggests that using class names is not an option, nor is there any logical reason that I can think of at the moment why that should be the case.

    Mike

  7. #7
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    mwinter: here's my full code.

    Code:
    <html>
    <head>
    <script type="text/javascript">
    var ie = document.all;
    var ns = document.getElementById && !ie;
    var x,y;
    var dobj;
    var isdrag = false;
    
    function contDrag(e){
    if(isdrag){
    dobj.style.left = ns ? tx + e.clientX - x : tx + event.clientX - x;
    dobj.style.top = ns ? ty + e.clientY - y : ty + event.clientY - y;
    return false;
    }
    }
    
    function initDrag(e){
    topelements = ns ? "HTML" : "BODY";
    fobj = ns ? e.target : e.srcElement;
    while(fobj.tagName != topelements && fobj.className != "drag"){
    fobj = ns ? fobj.parentNode : fobj.parentElement;
    }
    if(fobj.className == "drag"){
    isdrag = true;
    dobj = fobj;
    tx = parseInt(dobj.style.left + 0,10);
    ty = parseInt(dobj.style.top + 0,10);
    x = ns ? e.clientX : event.clientX;
    y = ns ? e.clientY : event.clientY;
    document.onmousemove = contDrag;
    }
    var cart = document.getElementById("cart");
    }
    
     function getElementsById(sId)
     {
        var outArray = new Array();	
    	if(typeof(sId)!='string' || !sId)
    	{
    		return outArray;
    	};
    	
    	if(document.evaluate)
    	{
    		var xpathString = "//*[@id='" + sId.toString() + "']"
    		var xpathResult = document.evaluate(xpathString, document, null, 0, null);
    		while ((outArray[outArray.length] = xpathResult.iterateNext())) { }
    		outArray.pop();
    	}
    	else if(document.all)
    	{
    		
    		for(var i=0,j=document.all[sId].length;i<j;i+=1){
    		outArray[i] =  document.all[sId][i];}
    		
    	}else if(document.getElementsByTagName)
    	{
    	
    		var aEl = document.getElementsByTagName( '*' );	
    		for(var i=0,j=aEl.length;i<j;i+=1){
    		
    			if(aEl[i].id == sId )
    			{
    				outArray.push(aEl[i]);
    			};
    		};	
    		
    	};
    	
    	return outArray;
     }
    
    
    function termDrag(e){
    isdrag = false;
    with(document){
    targ = getElementById("cart");
    //obj = getElementById("drag");
    space = getElementById("coor");
    next = getElementById("nexcoor");
    }
    var obj = getElementsById("drag");
    
    targX = parseInt(targ.offsetLeft - 8);
    targY = parseInt(targ.offsetTop - 8);
    targH = parseInt(targ.offsetHeight);
    targW = parseInt(targ.offsetWidth);
    targXpW = targX + targW;
    targYpH = targY + targH;
    for(var i = 0;i < obj.length;i++){
    objX = parseInt(obj[i].offsetLeft);
    objY = parseInt(obj[i].offsetTop);
    objH = parseInt(obj[i].offsetHeight);
    objW = parseInt(obj[i].offsetWidth);
    objXpW = objX + objW;
    objYpH = objY + objH;
    }
    
    if(objX > targX && objY > targY && objX < targXpW && objY < targYpH){
    next.innerHTML = "Got It!!!";
    } else {
    next.innerHTML = "";
    }
    }
    document.onmousedown = initDrag;
    document.onmouseup = termDrag;
    
    </script>
    <style type="text/css">
    div.drag {
    width: 100px;
    height: 100px;
    background-color: red;
    position: relative;
    z-index: 200;
    }
    div.drag:active {
    cursor: move;
    }
    div.cart {
    width: 500px;
    background-color: green;
    height: 350px;
    position: relative;
    left: 300px;
    }
    </style>
    </head>
    <body>
    <div id="drag" class="drag">television</div>
    <div id="drag" class="drag">table</div>
    <div id="drag" class="drag">sink</div>
    <div id="cart" class="cart"></div>
    <div id="coor"></div>
    <div id="nexcoor"></div>
    </body>
    </html>
    That shall be enough to tell you what I am trying to do.

    The problem I am facing with this is the script displays "Got It!!!" only when the first div is dropped in the target.

  8. #8
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Give up on the browser detection! It is a flawed concept. Use feature detection instead.

    You cannot have multiple elements with the same ID. It will simply not work. You could build a (or use a prebuilt) getElementsByClassName function, or you could use a similar method, but with different IDs through which one can loop:
    Code:
    function getElementsByIDStart(base) {
      for(var i = 1, ret = [], e; e = document.getElementById(base + i); ++i)
        ret.push(e);
    }
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  9. #9
    Join Date
    Aug 2005
    Posts
    971
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Twey, Thanks for your reply but how do I use feature detection?? I haven't even used half of the browsers on the web and don't know almost all the features.

    If I don't have multiple IDs then how do I detect if the user has dropped item in the target(as that code uses ids to detect elements' offsetwidth and offsetheight)?? If I must not use IDs then how do I know if the user has dropped the item in the target, I have no idea??

    And what does this code do??

    Code:
    function getElementsByIDStart(base) {
      for(var i = 1, ret = [], e; e = document.getElementById(base + i); ++i)
        ret.push(e);
    }

  10. #10
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    The beauty of feature (object) detection is that it doesn't require that you know browsers. It simply tests to see whether or not the object you are about to use exists or not, ex:

    Code:
    var theEl=document.all? document.all[id] : document.getElementById? document.getElementById(id) : null;
    Last edited by jscheuer1; 08-04-2006 at 07:12 PM. Reason: spelling
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

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
  •