Jump to content

Imitating the index property


lemmin

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.