/    Sign up×
Community /Pin to ProfileBookmark

Tricky point – regarding Lists (li & ul)

Got a tricky question – don’t even know if it’s possible:

I am using the yui scripts, specifically drag and drop for lists.

I got it to work great.

But what I want to do is store in my mysql database the new position when I drag and drop so that the next time I view the page it displays the correct sort order based on how I arranged the list with drag and drop.

[B]Is there a way in javascript to read the position of a list item?[/B]

The solution I had thought up was to use JS to read this number and then update the record using AJAX.

Does this seem right?

Thanks

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@CuppyTeaDec 09.2008 — Got a tricky question - don't even know if it's possible:

I am using the yui scripts, specifically drag and drop for lists.

I got it to work great.

But what I want to do is store in my mysql database the new position when I drag and drop so that the next time I view the page it displays the correct sort order based on how I arranged the list with drag and drop.

[B]Is there a way in javascript to read the position of a list item?[/B]

The solution I had thought up was to use JS to read this number and then update the record using AJAX.

Does this seem right?

Thanks[/QUOTE]


Yes.

document.getElementById("theUl").childNodes is your new best friend. He is an array which contains the <li> elements in the order they are displayed,
Copy linkTweet thisAlerts:
@saturnprodsauthorDec 09.2008 — Yes.

document.getElementById("theUl").childNodes is your new best friend. He is an array which contains the <li> elements in the order they are displayed,[/QUOTE]


Thanks! Do you know of any link or page where I can learn more about this?
Copy linkTweet thisAlerts:
@CuppyTeaDec 12.2008 — Thanks! Do you know of any link or page where I can learn more about this?[/QUOTE]

Humm.. you might have found out yourself by now, but if not....

I don't know any specific "amazing" resource- prehaps google will help.

Say you have....

[CODE]<script>
window.onload = init;

function init() {
for(var i=0;i<document.getElementById("nav").childNodes.length;i++){
alert(document.getElementById("nav").childNodes[i].innerHTML);
}
}
</script>
</head>

<body>
<ul id="nav">
<li>List Element</li>
<li>List Element</li>
<li>List Element</li>
<li>List Element</li>
<li>List Element</li>
</ul>
</body>
</html>
[/CODE]


as an example.

All "childNodes" is, is an array of the objects contained within the tags of the element you are getting the childNodes for.

(childNodes is an array of OBJECTS - not Id's or anything).

Hope that slightly helps...
Copy linkTweet thisAlerts:
@mrhooDec 12.2008 — document.getElementById("theUl").childNodes will include a textnode for each newline in the source, except in IE.

Better to use document.getElementById("theUl").getElementsByTagName('li')
×

Success!

Help @saturnprods spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...