Remove links using the each() function in Javascript

My website is built on prestashop, and I have a dropdown menu with various clickable categories. However, I want to remove the links from the "Marques" and "Les Gammes" categories and only keep the text displayed. To achieve this, I am using the each() function to target all the categories. The issue I am facing is that it returns an array within the li and the nested ul inside the li.

If you'd like to see the code in action, check out this JSFiddle link.

Below is the JavaScript code snippet:

$('jms-mega-menu').ready(function () {
  // Target each div
  $('.notlink').each(function () {
      // Get the content
      var str = $(this).text();
      $(this).html(str);
   });
});

Here is a snippet of the HTML code structure. For the complete code, please refer to the JSFiddle link.

<ul jms-mega-menu>
 <div>
  ...
   <ul class="mega-nav level1">
    <li class=" haschild group notlink"><a id="item-8" href="#">Marques</a>
     <ul>
      <li><a id="item-9" href="#">Apple</a></li>
      <li><a id="item-10" href="#">Samsung</a></li>
     </ul>
    </li>
   </ul>
   <ul>
   ...

Answer №1

To resolve this issue, one solution is to utilize the unwrap function to eliminate the a tag wrapping around the text. It's important to note the specific implementation of .notlink > a in order to target only anchor elements that are direct descendants of the .notlink class without affecting nested lists:

$('.notlink > a').contents().unwrap();

Alternatively, another approach that offers more flexibility is to employ the replaceWith method. This method allows for additional modifications to be made to the text if necessary:

$('.notlink > a').replaceWith(function(){
    return $(this).text();   
});

With this method, we simply replace the anchor tags with the text contained within them.

http://jsfiddle.net/orvrj7qs/5/

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 is the best way to position a popup div in CSS?

Currently, I am in the process of developing a website that displays DVD details when hovering over an image, similar to what is shown in picture 1. However, I've encountered an issue where the content gets cut off for DVDs located on the right side o ...

Toggle draggable grid in jQuery

Imagine I have a grid set up using the following code: $( "#dragIt" ).draggable({ grid: [ 15, 15 ] }); Now, there is a checkbox located below the div. Is there a way for me to switch the grid on and off by toggling the checkbox? I've searched the of ...

Tips for evaluating an array of objects in JavaScript

Welcome to the world of coding! Here's a scenario with an array: [ { "question1": "Apple", "question2": 5, "question3": "Item 1" }, { "question1": ...

Learn how to dynamically disable a button based on the input state matching an email pattern!

I'm facing an issue with my login form that has 2 input fields and a login button. One of the input fields requires a valid email pattern. If any of the input fields are left empty, the login button becomes disabled. However, when an incorrect email p ...

Color key in square shape for graph legend

I am looking for legend colors in square shape, but I don't want them to appear as square boxes on the graph. https://i.stack.imgur.com/Of0AM.png The squares are also showing up on the graph, which is not what I want. https://i.stack.imgur.com/Az9G ...

What is the best way to switch the site header in JavaScript or jQuery?

I have created a Bootstrap menu design and I am looking to add a sliding functionality to it. My goal is to hide the menu when scrolling down and display it when scrolling up (slide-down / slide-up). For implementing this feature, I plan to utilize jQuery ...

Can anyone recommend a drag-and-drop feature in Javascript that functions seamlessly on both iPad and PC devices?

Can anyone recommend a JavaScript plugin that allows for drag and drop functionality on both touch and mouse enabled devices, including IOS, Android, PC, and Mac? For touch-enabled devices, I found an example here: And for mouse-enabled devices, there is ...

Activate the JavaScript function associated with the link's href when the document is fully loaded

Can the javascript function of an anchor tag be triggered during page load? Check out my JSFiddle <a id="WebTree" href="javascript:alert('testing')">click me</a> $(document).ready(function(){ $('#WebTree').trigger(& ...

Tips for implementing controlled components in Vue to update values in the parent component object

Utilizing controlled components, I am able to emit the selected value. For example, // app-select.vue <v-select :items="[1,2,3]" @change="$emit('input', $event)"></v-select> // parent-component.vue <app-sele ...

Navigating the DOM in real-time with jQuery using the parent()

$(".hovertip").parent().live('hover', function() { ... It appears that the code above is not being recognized. Also, the following code does not seem to be effective: $(".hovertip").parent().live({ mouseenter: function() { ... Are ...

The getelementbyid function is unable to locate the specified button identifier

As I dive into the world of Javascript, I wanted to create a simple input form with a corresponding response field on my website. However, I encountered an issue where using a basic submit button caused the page to refresh and erase the textfields before ...

Outputting a variable using javascript

My challenge is to efficiently print the contract variable, which contains HTML content fetched from the server. How can I achieve this easily? I want the print window in Chrome to display the document with the contents of my variable when I click a button ...

Leverage the retrieved JSON data from the database within an HTML template using the Play

Currently, I am facing a challenge with implementing a login feature in my project. I am struggling to figure out how to showcase the JSON string that is returned on the webpage. Below is a snippet of my code: Security.java: public Person webLogin(Perso ...

How can one resolve the error message that says "WebDriverError: Connection refused"?

I am facing an issue with running Protractor tests on my local machine. A few days ago, everything was working fine but now I am unable to run the tests even after rebooting Ubuntu. Here are the versions of different components: $cat /etc/issue Ubuntu 14. ...

Prolong the Slide Duration in Slider Pro Slideshow

I am currently utilizing Slider Pro from bqworks.com for a slideshow with automated cycling on my website. I would like to adjust the display duration of each image in the slideshow. The documentation mentions a property called "slideAnimationDuration," wh ...

"Exploring the world of jQuery and the array data structure

I'm attempting to target and access all inputs with the specified class: validate['number'] However, in some cases, the classes may appear as follows: validate['required', 'number'] My goal is to identify all elements ...

Utilize JSON to populate data in a switch case scenario

I am attempting to implement switch cases using JSON. How can I load the data into the switch statement? Here is the desired output: $(".custom-menu li").click(function(){ switch($(this).attr("data-action")) { case "Science": $('.abc:e ...

Repetitive calling of a Node.js function

Currently, I am developing using the nodejs express framework. On my webpage, there are two buttons: 1) Submit, which triggers the following function: router.get('/record_enrich_quick/:quick', function(req, res) { console.trace(); var j ...

Handling Asynchronous API Requests with AngularJS

I am facing an issue with displaying data from API in my files foo.js and foo.html. While fetching information asynchronously in foo.js, I store it in an object that should be accessible in foo.html. However, despite receiving the data correctly, I am una ...

The style was declined from being applied due to its MIME type, which was identified as 'text/html'

Currently, I am working on VueJS with Webpack and SCSS. I am facing a problem when attempting to import the SCSS file into the component using the following code: <style type="scss"> @import "/sass/pages/_initial.scss";</style> Upon doing so, ...