lemmin Posted June 3, 2010 Share Posted June 3, 2010 I have a list of DIVs in a DIV container and I want to be able to find out what index a specific div is in its parent's children collection. The other way around is easy. If I need to reference the 5th object in the container I simply use container.children[4]. But say I want the onclick event of one of those children to return its index number. This is where I am having trouble. The OPTION tag has an attribute called index that does just that, but I don't think I can format a select box to look the way I want it. Does anyone know of a tag that allows this attribute or a way to imitate its functionality? Or even how to format a select box to not look like a select box? Thanks for any help. Link to comment https://forums.phpfreaks.com/topic/203786-imitating-the-index-property/ Share on other sites More sharing options...
ignace Posted June 3, 2010 Share Posted June 3, 2010 .indexOf()? Link to comment https://forums.phpfreaks.com/topic/203786-imitating-the-index-property/#findComment-1067328 Share on other sites More sharing options...
lemmin Posted June 4, 2010 Author Share Posted June 4, 2010 indexOf() is a string function for finding substrings, so that won't work for what I am trying to do. Any other ideas? Thanks for the help. Link to comment https://forums.phpfreaks.com/topic/203786-imitating-the-index-property/#findComment-1067446 Share on other sites More sharing options...
haku Posted June 4, 2010 Share Posted June 4, 2010 Something like this maybe: this.parentNode.indexOf(this) Link to comment https://forums.phpfreaks.com/topic/203786-imitating-the-index-property/#findComment-1067499 Share on other sites More sharing options...
ignace Posted June 4, 2010 Share Posted June 4, 2010 indexOf() is a string function for finding substrings, so that won't work for what I am trying to do. The implementation of indexOf() depends on the object you are calling, like haku pointed out. When it's a string it'll search for sub-strings, when it's a nodelist it will return the index of a node. Link to comment https://forums.phpfreaks.com/topic/203786-imitating-the-index-property/#findComment-1067548 Share on other sites More sharing options...
lemmin Posted June 4, 2010 Author Share Posted June 4, 2010 Is this function proprietary to some browser? The Javascript documentation that I use describes indexOf() as ONLY being a method of the String object. I tested the following code: <html> <head> <script type="text/javascript"> function testit(obj) { alert(obj.parentNode.indexOf(this)); } </script> </head> <body> <div> <div></div> <div onclick="testit(this)">hi</div> <div></div> </div> </body> </html> (I also tried using it on a Collection object.) And received this error: obj.parentNode.indexOf is not a function I am using Firefox 3.6.3 Thanks for the help. This function is exactly what I am looking for if I could just get it to work! Am I doing something wrong? Link to comment https://forums.phpfreaks.com/topic/203786-imitating-the-index-property/#findComment-1067998 Share on other sites More sharing options...
lemmin Posted June 8, 2010 Author Share Posted June 8, 2010 Any other ideas? I am guessing that there is a way to make this work with HTML properties instead of Javascript. That is why I posted this in the HTML help forum originally. My reasoning is based on the index property of the option element. Another element that has a similar property to this is the table element. You can find out what cell is being clicked on based on its properties. I might actually be able to format table cells to look the way I want, but I would like to know if there is a different element or workaround that would make the code less cumbersome. Maybe a certain list element has this functionality. Thanks again for the help. Link to comment https://forums.phpfreaks.com/topic/203786-imitating-the-index-property/#findComment-1069601 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.