dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18

Thread: div elements & its length

  1. #1
    Join Date
    Apr 2008
    Location
    Pune,India
    Posts
    21

    div elements & its length

    Hi Gurus !

    I am an iPhone developer.
    I have an xml file called game.xml

    The structure is as follows:-

    Code:
    <?xml version="1.0" encoding="iso-8859-1"?>
    <games>
    
    
    <game>
    <question>question1</question>
    <answer>answer1</answer>
    </game>
    
    <game>
    <question>question2</question>
    <answer>answer2</answer>
    </game>
    
    <game>
    <question>question3</question>
    <answer>answer3</answer>
    </game>
    
    </games>

    1. Am displaying questions in my page & for answers -> I have created Div elements & my own keyboard from where the user selects the key to insert the correct answer

    2.This is how I have created the div for each character in answer node

    Code:
    for(j=0;j<arr[i][1].length;j++)
    {   
    		var ind2 = eval(j+1);
    		gameVar+="<td> <div name=\"ans"+ind1+"_"+ind2+"\" id=\"ans"+ind1+"_"+ind2+"\" width=\"45px\" height=\"45px\" STYLE=\"position:relative; overflow:hidden; left:"+left2+"px; border-style:solid; border-color:#999999 \" onClick=\"focuset('"+ind1+"',id);\"  onKeyUp=\"OnKeyPress(this, event ,i);\" onFocus=\"focuset('"+ind1+"',id);\" onKeyPress=\"if (event.keyCode==8){removeTextById(id)} \">&nbsp;&nbsp;&nbsp;</div></td>";
    		left2=left2+5;
    }
    3. My problem is that am not able to get the total no. of div elements created (each character div)

    m using (document.game.elements.length) to find out the length of my answer char div which is actually 21

    answer1 = 7
    answer2 = 7
    answer3 = 7 i.e 21

    4. Can somebody help me how do I get the total no of DIV element using JavaScript
    Last edited by PeejAvery; April 23rd, 2008 at 08:00 AM. Reason: Added code tags.

  2. #2
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,943

    Re: div elements & its length

    Instead, you should use document.getElementsByTagName('div'). That will return an array of all the div elements. Then you can just use the length property.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  3. #3
    Join Date
    Apr 2008
    Location
    Pune,India
    Posts
    21

    Re: div elements & its length

    ok fine thanks
    I solved it !

    Now pls have a look at this..

    Code:
    for(j=0;j<arr[i][1].length;j++)
    {   
    		var ind2 = eval(j+1);
    		gameVar+="<td> <div tabindex=\""+eval(++tabIndVal)+"\" name=\"ans"+ind1+"_"+ind2+"\" id=\"ans"+ind1+"_"+ind2+"\" width=\"45px\" height=\"45px\" STYLE=\"position:relative; overflow:hidden; left:"+left2+"px; border-style:solid; border-color:#999999 \" onClick=\"focuset('"+ind1+"',id);\"  onKeyUp=\"OnKeyPress(this, event ,i);\" onFocus=\"focuset('"+ind1+"',id);\" onKeyPress=\"if (event.keyCode==8){removeTextById(id)} \">&nbsp;&nbsp;&nbsp;</div></td>";
    		left2=left2+5;
    }
    I had given tabindex value to my div & now I want value of tabindex which I am tring to get it this way as below

    Code:
    alert(document.getElementById(divName).tabindex);
    Am getting values as "undefined"

    Please help.

  4. #4
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,943

    Re: div elements & its length

    Well, where have you declared divName? Also, I believe that tab indexes only work input elements (input & textarea).
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  5. #5
    Join Date
    Apr 2008
    Location
    Pune,India
    Posts
    21

    Re: div elements & its length

    Well its not divName ..it is divId which is the ID which am giving to the div..

    For eg:-
    Code:
    alert(document.getElementById("ans"+ind1+"_"+ind2).tabindex);
    About the tabindex ..I also have a doubt if it works with DIV element
    Anyway is there any solution where in I could get the tabIndex

    or anyother property similar to that

    Thanks

  6. #6
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,943

    Re: div elements & its length

    Notice that the I is capitalized. So the following code should work.

    Code:
    alert(document.getElementById("ans"+ind1+"_"+ind2).tabIndex);
    BTW...According to Microsoft the div elements have a tab index.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  7. #7
    Join Date
    Apr 2008
    Location
    Pune,India
    Posts
    21

    Re: div elements & its length

    1. Actually I am implementing this for Safari Browser and I guess DIV tabIndex is not supported by Safari
    cos still am getting the result as "undefined" with the alert you mentioned above

    2. I am setting a char to the divs using innerHTML as below
    There are 'n' no of divs

    Code:
    document.getElementById(divId).innerHTML="&nbsp;"+kid+"&nbsp;";
    where 'kid' is the key selected by user

    Now here, I ant to get those divs whose innerHTML="" i.e div which has no char set into it as innerHTML

    I am tring to identiy such divs like below but unable to get them

    Code:
    if(document.getElementById(divId).innerHTML=="none")
    	{
    	  alert("no character")
    	}
    I have also tried as below
    Code:
    if(document.getElementById(divId).innerHTML=="")
    	{
    	  alert("no character")
    	}
    How do I identify such divs ?
    Please help..
    Last edited by nitin.randive; April 25th, 2008 at 01:44 AM.

  8. #8
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,943

    Re: div elements & its length

    I too am a Safari user. Div elements do have a tabIndex if you set them, else they will return undefined. You can see that with the following code.

    Code:
    <div id="test">&nbsp;</div>
    
    <script type="text/javascript">
    alert(document.getElementById('test').tabIndex); // returns undefined
    document.getElementById('test').tabIndex = 5;
    alert(document.getElementById('test').tabIndex); // returns 5
    </script>
    To get all the empty div elements, you can use the following.

    Code:
    var divs = document.getElementsByTagName('div');
    for (i = 0; i < divs.length; i++) {
      if (divs[i].innerHTML == '') {
        // this is an empty div
      }
    }
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  9. #9
    Join Date
    Apr 2008
    Location
    Pune,India
    Posts
    21

    Re: div elements & its length

    Thanks !
    I was able to give tabIndex to my divs !

  10. #10
    Join Date
    Apr 2008
    Location
    Pune,India
    Posts
    21

    Question Re: div elements & its length

    Well the problem of identifying the div with some space is bugging me a lot
    This is how I put a space into my div :-

    Here divId is the ID given to my div element
    Code:
    document.getElementById(divId).innerHTML = '&nbsp';
    and now am not able to find this div for which I am using the code as below
    Code:
    if(document.getElementById(divId).innerHTML==' ') //fails
    {
       // some action
    }
    When I use some value instead of &nbsp, I am able to find it
    for eg:-
    Code:
    document.getElementById(divId).innerHTML = '0';
    
    if(document.getElementById(divId).innerHTML=='0') // succesful
    {
       //some action
    }
    but I dont want to use the above ..I want to put some space into my div and then identify the empty divs (with space)

    Please help me..its urgent
    Last edited by nitin.randive; April 29th, 2008 at 05:06 AM.

  11. #11
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,943

    Re: div elements & its length

    Space and &nbsp; are not the same thing, so that code will fail anyway. &nbsp; is an html entity which makes the browser believe there is a space there and should not break. If you are looking for &nbsp; you must search for it, not a space.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  12. #12
    Join Date
    Apr 2008
    Location
    Pune,India
    Posts
    21

    Re: div elements & its length

    oh ok
    but still as u said I tried out searching for &nbsp for I havent got any results

    Here are the details:-
    Code:
    document.getElementById(divId).innerHTML = '&nbsp;&nbsp;&nbsp;';
    
    if (document.getElementById(divId).innerHTML=='&nbsp;&nbsp;&nbsp;')
    {
      // some action
    }
    Even this is not working
    Whats wrong ??
    Please help

  13. #13
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,943

    Re: div elements & its length

    It works for me. Are you sure you aren't calling this before the div is loaded by the browser? Check the Error Console using Firefox to see.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  14. #14
    Join Date
    Apr 2008
    Location
    Pune,India
    Posts
    21

    Re: div elements & its length

    Oh !
    Its working fine with firefox
    n not with safari

    Any comments

  15. #15
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,943

    Re: div elements & its length

    Ah yes. I forgot you were a Safari user. The problem here is that Safari treats it completely differently. You can see that by my test script below.

    Code:
    <div id="test"></div>
    <div id="test2" style="border: 1px solid #000000"></div>
    
    <script type="text/javascript">
    document.getElementById('test').innerHTML = '&nbsp;&nbsp;&nbsp;';
    if (document.getElementById('test').innerHTML == '&nbsp;&nbsp;&nbsp;') {
      alert('HTML Entity');
    }
    if (document.getElementById('test').innerHTML == '   ') {
      alert('Space');
    }
    else {
      alert('"' + document.getElementById('test').innerHTML + '"');
      // even copying this does not return true
      document.getElementById('test2').innerHTML = document.getElementById('test').innerHTML;
    }
    </script>
    Is this still part of finding the "empty" div tags? If so...I think your only way around this would be to use regular expressions.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

Page 1 of 2 12 LastLast

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)