Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / October 2007

Tip: Looking for answers? Try searching our database.

Javascript question

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Tem - 13 Oct 2007 08:29 GMT
I need to write a script that will reverse the ordering of the <span> tags
programatically.

<div class="paragraph">
<span class="line">line 1</span>
<span class="line">line 2</span>
<span class="line">line 3</span>
</div>

when the function runs the html display should be

<div class="paragraph">
<span class="line">line 3</span>
<span class="line">line 2</span>
<span class="line">line 1</span>
</div>

Can this be done with javascript? Sorry I'm new to programming.
Nathan Sokalski - 13 Oct 2007 17:38 GMT
If you want to do this using JavaScript I would suggest using an array, and
iterating through it. This way you can simply increment or decrement the
index. However, unless it is critical that this be done client-side, I would
suggest that you do it server-side so that you can avoid unnecessary messy
JavaScript (I am a fan of JavaScript, but if it does not improve the
appearance, performance, or usability of a page, I prefer server-side). If
it is critical and you need help creating the script, let me know. Good
Luck!
Signature

Nathan Sokalski
njsokalski@hotmail.com
http://www.nathansokalski.com/

>I need to write a script that will reverse the ordering of the <span> tags
>programatically.
[quoted text clipped - 14 lines]
>
> Can this be done with javascript? Sorry I'm new to programming.
Tem - 13 Oct 2007 19:51 GMT
I thought of that too but I want to save users from redownloading the page.

Here's my thinking

Get all objectsin paragraph
Save objectsin to an array
Reverse the array
Push the objectsin back to paragraph

if you can help me translate this into javascript that would be great
thanks

> If you want to do this using JavaScript I would suggest using an array,
> and iterating through it. This way you can simply increment or decrement
[quoted text clipped - 22 lines]
>>
>> Can this be done with javascript? Sorry I'm new to programming.
Nathan Sokalski - 13 Oct 2007 21:37 GMT
Try this:

<div id="divParagraph" class="paragraph">
<span class="line">line 1</span>
<span class="line">line 2</span>
<span class="line">line 3</span>
</div>

var lines[];
var index=0;
var original=document.getElementById('divParagraph').innerHtml;
var index1=original.indexOf('<span');
var index2=original.indexOf('<span',index1+1);

while(index2!=-1)
{
lines[index]=original.slice(index1,index2-1);
index1=original.indexOf('<span',index1+1);
index2=original.indexOf('<span',index2+1);
index++;
}
lines[index]=original.slice(original.lastIndexOf('<span'),original.length-1);

document.getElementById('divParagraph').innerHtml='';
for(line in lines)
{
Alexey Smirnov - 13 Oct 2007 21:47 GMT
> I thought of that too but I want to save users from redownloading the page.
>
[quoted text clipped - 7 lines]
> if you can help me translate this into javascript that would be great
> thanks

Tem, you can also create an array from the beginning and use it to
populate a paragraph and then to sort an elements...
bruce barker - 13 Oct 2007 23:54 GMT
<script>
    function cloneNodeList(list)
    {
        var a = new Array();
        for (var i=0; i < list.length; ++i)
            a[i] = list[i];
        return a;
    }
    function reverseDiv()
    {
        var div = document.getElementById('foo');
        var spans = cloneNodeList(div.childNodes);
        for (var i=0; i < spans.length; ++i)
            div.removeChild(spans[i]);
        for (var i=spans.length-1; i >= 0; --i)
            div.appendChild(spans[i]);
    }
    </script>
<div class="paragraph" id="foo" onclick="reverseDiv();">
 <span class="line">line 1</span>
 <span class="line">line 2</span>
 <span class="line">line 3</span>
</div>

-- bruce (sqlwork.com)

> I need to write a script that will reverse the ordering of the <span>
> tags programatically.
[quoted text clipped - 14 lines]
>
> Can this be done with javascript? Sorry I'm new to programming.
Tem - 14 Oct 2007 01:04 GMT
Thank you

>    <script>
>     function cloneNodeList(list)
[quoted text clipped - 40 lines]
>>
>> Can this be done with javascript? Sorry I'm new to programming.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.