Swapping link positions with jQuery

Is it possible to rearrange links using jQuery?

Under the navigation menu, there will be content div for each tab. The selected link should always be positioned next to the first "sixrevision" link.

The code snippet is shown below:

   <ul id="crumbs">
   <li><a href="#" class="selected">Home</a></li>
   <li><a href="#">product 1</a></li>
   <li><a href="#">product 1</a></li>
   <li><a href="#">product 1</a></li>   
   /ul>

This set up will create an AJAX tab system with tabs that resemble breadcrumbs. The goal is to ensure that the "selected" item is always placed next to the first item in the list.

Answer №1

To make the .selected element the first child within the #crumbs container, use the .prependTo() method as shown below:

$("#crumbs .selected").prependTo("#crumbs");

Answer №2

My approach is slightly different, as demonstrated below and on JSFiddle (try clicking the links in the example)

The main advantage of this method is that changes can be made using only CSS. There is no need to modify HTML or use JavaScript unless it's a server-side change. I believe this approach is cleaner and more manageable in the long term.

NOTE: The HTML has been modified slightly so that the containing li element applies the 'selected' class, rather than the a element. I've found that adding 'selected' and 'active' classes to the containing elements instead of the clicked element offers more flexibility.

HTML

 <ul id="crumbs">
   <li><a href="#">XHTML</a></li>
   <li class="selected"><a href="#">Javascript</a></li>
   <li><a href="#">Flash</a></li>
   <li><a href="#">SEO</a></li>   
</ul>

JS

$(function(){
    $('ul#crumbs li a').bind('click',crumbClick)
});

function crumbClick(){
    $('ul#crumbs .selected').removeClass('selected');
    $(this).parents('li').addClass('selected');
    return false;
}

CSS [for example]

/* RESET */
ul#crumbs, ul#crumbs li { margin:0;padding:0;}

ul#crumbs li a {display:inline-block;padding:3px 4px;}

ul#crumbs li {list-style:none;display:inline;}

ul#crumbs li.selected {float:left;}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for utilizing Bootstrap validator to check the size and type of files

Currently, I am utilizing the Bootstrap validator to validate my form. However, I have encountered a challenge with an input field for files where I would like to implement custom validation for file types and sizes (such as pdf, docx, doc...). <div c ...

The save functionality is not working due to a JavaScript issue

Having an issue with the save button functionality. The JavaScript code specified for the button seems to interfere with its ability to save information. Interestingly, when I remove the JavaScript it works perfectly and saves the data as intended. (fun ...

The JQuery TextNTags plugin eliminates tag formatting once the trigger syntax has been modified

I have incorporated the JQuery TextNTags plugin into my web application. Here is the original code snippet: $.browser = { webkit: true }; $(function () { $('textarea.tagged_text').textntags({ triggers: {'!': { ...

The JQuery AJAX call to a PHP script results in a 500 error response code

I seem to be encountering a server error that I can't quite pinpoint. It's frustrating and I'm unsure of what's causing it. Here’s the breakdown of my file structure: project_manager >ajax >register.php >cla ...

Using PHP and MySQL to create checkboxes populated with data retrieved from a database, and then submitting two parameters

Hey there, I've been struggling with this issue for quite some time. I'm trying to create a list with checkboxes populated from a database that includes [checkbox of car id and value][brand][model]. The goal is to select two cars from the list, ...

As I scroll past the top or bottom of the page, the body's background color transitions to a new shade

I am encountering an issue with my next.js app where the body background color is set to black. Here is the code in globals.css: body { background-color: black; padding: 0; margin: 0; font-family: 'Titillium Web', sans-serif; } However, ...

Retrieve dual JSON objects simultaneously using the AJAX method in jQuery

I am facing an issue where I am trying to return two JSON objects, but only one is being received. The variable 'success' is displaying as a string 'success' when I try to alert it; however, in Firebug, its value is true. Therefore, the ...

In search of an effortless solution to fetch the hostname, computer name, and IPV6 address utilizing ASP or VB6

In search of an effortless method to retrieve the computer name, host name, and ipv6 ipaddress using vb6, asp, or jQuery. The primary motivation for this task is to ensure comprehensive logging of crucial security information. ...

Fixing content at the bottom inside a container with Bootstrap 4

My app is contained within a <div class="container">. Inside this container, I have an editor and some buttons that I always want to be displayed at the bottom of the screen. Here's how it looks: <div class="container> // content here.. ...

Ways to avoid a bootstrap column from wrapping onto a new line when the parent width is decreased

https://i.sstatic.net/gKdAN.png I attempted to decrease the form width by applying padding on both sides of the form using 'px-5'. However, when I implemented this change, the column holding the '#lastName' input shifted to a new line. ...

html form submission error - please refresh

After submitting a form on my page, I keep encountering an error every time I try to refresh the page. I've attempted several solutions that I came across online, but none of them have resolved the issue as I continue to face this error on different ...

Outputting PHP code as plain text with jQuery

My aim is to set up a preview HTML section where I am encountering a difficulty. I am struggling to display PHP code when retrieving and printing it from a textarea in the HTML. Here are my current codes, This is the HTML area where the textarea code will ...

Creating a visually appealing Django filter form with custom styling in HTML

Currently, this is how my django filter form appears: Below is the html code I am using: <form action="" method="get" class="form-inline"> {{myfilter.form|bootstrap}} <button class="btn btn-primary&q ...

Is there a bug in jQuery draggable that causes the draggable box to exceed the container boundaries?

Follow the link to see this code : HTML Code <div class="draggable_container"> <div id="draggable_1" class="draggable"> <div class="exp"><!-- --></div> </div> </ ...

Tips on how to navigate to the end of a div that has been created through ng-repeat in Angular JS with the

Here is the template code I am working with: <div class="chatbox" id="mailBody" > <div class="panel-body" ng-repeat="mail in mails"> <div class="m-b-none" ng-if="mail.inboxMessageType == 1"> <a href class="pull-left ...

The functionality of removing a class on the body element is ineffective when using pagepiling.js

After creating a website using pagepiling.js, I implemented a script that adds the 'active' class to the section currently in view. My goal was to add a specific class to the body when my section1 is considered active. Here's the initial app ...

How can I expand the notification pop-up in R Shiny?

I'm trying to figure out how to expand the notifications in R Shiny because right now they are cutting off longer error messages. Any suggestions? library(shiny) ui <- fluidPage( actionButton("test", "Test") ) server <- f ...

What is the best way to make an input text the focus when an item on a dropdown menu is clicked?

I have a website with a dropdown menu and an input box. I want the user experience to be more seamless by automatically focusing the mouse cursor inside the input box when they click on an option in the dropdown menu. This way, users can start typing right ...

Develop fresh JavaScript code using JavaScript

Is it possible to dynamically create a new row in an HTML table by clicking on a button? Each row contains multiple input fields. <script type="text/javascript> row = 2; specific_row_id = new Array(); $(document).ready(function() { $(".change_1 ...

Issues with the animation of the navbar menu button are preventing it from functioning

I have been attempting to incorporate animation when the navbar-button is toggled on smaller screen sizes. Inspired by the design of .navbar-toggle.larr in this particular template, I tried to implement a similar effect. /* ANIMATED LEFT ARROW */ .navbar- ...