The functionality of using variables in conjunction with the .insertAfter method appears to be

As a novice coder with limited English skills, I apologize in advance. Here is the CSS code snippet I am working with:

#wrapper{
    width: 200px;
    height: 300px;
    border: solid 1px #000; 
    overflow: visible;
    white-space: nowrap;
}
.page{
    display: inline-block;
    margin-right: -4px;
    width: 200px;
    height: 300px;
}
#page1{
    background-color: #CF9; 
}
#page2{
    background-color: #FC9;
}
#page3{
    background-color: #99F;
}
h1{
    color: white;   
}

The HTML structure looks like this:

<div id="wrapper">
<div class="page" id="page1"><h1>page1</h1>
<ul>
<li><a href="#page1">page 1</a></li>
<li><a href="#page2">page 2</a></li>
<li><a href="#page3">page 3</a></li>
</ul>
</div>
... (HTML for page 2 and 3 here)
</div>

Additionally, there's some Jquery code:

$( document ).ready(function() {
    $(".page a").click(function(){
        fromURL=$(this).closest(".page").attr("id");
        toURL = $(this).attr("href");
        $(toURL).insertAfter(fromURL);
        //$("#page3").insertAfter("#page1"); //this works
        })


    });

I have noticed that .insertAfter function does not work with variables. How can I overcome this issue?

Answer №1

The identifier fromURL lacks a hash symbol, causing it to be interpreted as an element name by jQuery, resulting in nothing being selected. You can convert it into an ID selector:

$(toURL).insertAfter("#" + fromURL);

However, considering you already have the element, why bother with IDs?

var page = $(this).closest(".page");
var toURL = $(this).attr("href");
$(toURL).insertAfter(page);

Remember to declare your variables properly. Implicit globals are not recommended. Enforcing strict mode and using tools like JSHint can prevent such issues.

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

What could be the reason for the lack of response from the jquery element on an

Recently, I've been working on a jQuery project with sticky headers. However, when I tested it on my iPad, I noticed a slight delay and the header jumping around the page. I'm wondering if this issue can be resolved within the code itself or if ...

Using ajax.actionlink to display the desired text

When using an ajax.actionlink @Ajax.ActionLink("Add Last Name", // <-- Text to display "AddTimeSeriesData", // <-- Action Method Name etc.... @id = "link") How can I extract the link text in JavaScript? I attempted to use $(&apo ...

Adding a custom source to the script tag in Angular 7

I am currently using angular cli to develop my web application. The app is being built in the dist folder as of now. This is the index.html file: <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>Adm ...

Having issues with the custom listing feature in Jquery UI auto complete not functioning as expected

I am facing an issue with my implementation of jquery UI autocomplete. The autocomplete functionality is not working as expected, despite the absence of any JavaScript errors. Below is the JSON data that I am working with: {"d":"[{\"label\":&bs ...

CSS code that causes the animated photo banner to reset instead of continuously looping back to the first

I followed a tutorial and created this banner using HTML and CSS. However, I encountered an issue where instead of the banner continuing in loops, it keeps resetting. Since we haven't covered JavaScript yet in university, I'm looking for help to ...

What is the best way to ensure my hover caption spans the entire size of the image?

I'm currently working on creating a hover caption that adjusts itself to fit the full width and height of an image even when it is resized. I attempted to implement a jQuery solution that I came across, but unfortunately, I am struggling to get it to ...

Do we need to include href in the anchor tag?

Why am I unable to display the icon within the <anchor> element without using the href attribute? The icon only appears when I set the href attribute to "". Even if I manage to show the icon by adding href="", adjusting the size with width and height ...

What could be causing the Bootstrap Navbar toggle icon button to not display navigation items when clicked?

Bootstrap has been a reliable tool for my projects until I encountered an issue in an Angular project. The navbar toggler icon button is visible, but clicking on it does not display the navigation items. <div> <nav class="navbar navbar-ex ...

Changing the event when a class is active in Vue3

Question I am looking for a way to trigger an event when the 'active' class is added to an element. How can I achieve this? I believe this could potentially be accomplished using a watcher method, but I am unsure how to watch for the applicatio ...

Tips for repairing the gray margins on the right and left sides of a webpage

Currently utilizing Bootstrap 5 and incorporating columns within the framework. However, encountering an issue where there is black space on the right side of the screen and certain content disappearing on the left side as demonstrated here. Here is the c ...

Creating Scroll Animations in Javascript - Mastering scrollIntoView Animation

When I click on a div, I was only able to bring it into view with the scrollIntoView function. It functions correctly and meets my expectations, but I am curious if there is a way to animate it and slow down the process. I attempted a suggestion found her ...

Prevent duplicate email submissions to the database by utilizing the jQuery validation plugin with AJAX

Hello everyone, I am currently utilizing the jquery validation plugin and it is functioning properly. However, I am encountering an issue where if an email already exists in the database, the form should not be submitted until a different valid email is ch ...

The link does not respond to the first click

I'm having an issue with the search feature on my website. The page includes an auto-focused text input element. As the user types, an ajax request is made and JQuery fills a div with search results. Each result is represented by an <li> elemen ...

How can I show a loading screen while making a synchronous AJAX call in Chrome?

Is there any method to show a loading screen in Chrome while using async:false in an AJAX call? The use of setTimeout poses several challenges when making multiple synchronous AJAX calls within the setTimeout function. Additionally, the loading indicator ...

Setting up Fancytree to Fetch Data via AJAX

I've been attempting to make it work for hours without success. I am currently trying to set up Fancytree with ajax. Here is my current progress: $(function(){ $("#tree").fancytree({ initAjax: {url: "/ajaxData.do", d ...

How come there is such a large space for clicking between my logo and the navigation bar, and what can be done to eliminate it?

* { box-sizing: border-box; padding: 0; margin: 0; } ul { list-style-type: none; } .nav-links, .logo { text-decoration: none; color: #000; } .logo { max-width: 80%; width: 20%; height: 20%; } .navbar img { width: 10%; height: auto; di ...

Is it possible to use calc with the display property set to table-cell?

I have created a structure using divs to mimic table elements for an entry form. My goal is to make the left column, where field labels are located, 50% wide with an additional 2 em space to accommodate an asterisk marking required fields. The right column ...

How can I create an asynchronous route in AngularJS?

I implemented route and ngView to display dynamic content, however I received a warning message: The use of Synchronous XMLHttpRequest on the main thread is deprecated due to its negative impact on user experience. For more assistance, please refer to ...

Ways to retrieve the most recent message on WhatsApp Web

I'm currently developing a JavaScript script for WhatsApp Web that will automate responses to messages in a specific group. Here is a snippet of my code: console.log('WhatsappWeb On'); function sleep(num){ setTimeout(num); } var eve ...

Ways to convert and download multiple iframes as PDF files

I have a webpage that displays content from five different pages using iframes. I am looking to convert the combined content of these iframes into a PDF after the entire page has finished loading. How can I achieve this? <iframe class="page-inner ...