Activate function when list item is clicked

My goal is to execute a function when users click on the li elements in the snippet below without relying solely on onclick. For instance, clicking on the first li element (with a value of 1) should trigger a function where the value "1" is passed as an argument.

Styled list control

..    
    <a href="#" class="path-nav-close"></a>
    <ul id='uItem'>
        <li><a href="#" class="launch" >1</a></li>
        <li><a href="#" class="launch" onclick="alert('2')">2</a></li>

...

https://jsfiddle.net/wL8vpuqt/5/

Answer №1

Check out this interactive example.

To stop the default behavior, include event.preventDefault() in your code:

$(document).ready(function() {
  $('ul#uItem li').click(function(event) {
    event.preventDefault();

    alert($(this).text());
  });
});

Hoping this solution is helpful for you.

$(document).ready(function() {
  $('ul#uItem li').click(function(e) {
    e.preventDefault();

    alert($(this).text());
  });
});
/* CSS properties */
.path-nav {
  -moz-transform: translateZ(0);
  -webkit-transform: translateZ(0);
  -o-transform: translateZ(0);
  -ms-transform: translateZ(0);
  transform: translateZ(0);
  position: fixed;
}

/* More CSS rules... */

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

Background image of HTML Iframe element does not display - Inline style block

https://i.stack.imgur.com/JW26M.png After setting the iframe's innerHTML using this line of code: iframedoc.body.innerHTML = tinyMCE.activeEditor.getContent(); The styles for the elements within the iframe are contained in an inline style block. Ho ...

Show each item in the list vertically, using a single unordered list but displaying them in

Is there a way to display list items vertically in multiple columns using only a single ul? I need the output to look like: a e i b f j c g d h Any suggestions on how to achieve this would be greatly appreciated. Thank you! ...

Simply click and drag a document from your file explorer directly into the desired text field to instantly generate a clickable

I am interested in dragging a file from our Windows server and dropping it onto a text area on a webpage. The link that would be generated will look something like this: <a href="\\fileserver\folder\pizza_2.pdf">filename.pdf< ...

Facebook news feeds displaying only the most recent 20 posts

I have my feeds stored in JSON format at this URL, but I am only able to see the last 20 posts. How can I retrieve all of my posts using JSON format? ...

The functionality for transferring ERC20 claim tokens seems to be malfunctioning

I am facing an issue with my contract while trying to execute the claimFreeToken function. Even though the contract does not have enough tokens, the function does not return an error and the token also does not get received. Can anyone point out where I ...

Error: The gulp-cssmin plugin encountered a TypeError because it attempted to read the property '0' of a null

I am attempting to condense my code, but I am encountering this error: D:\gulp-compiler\node_modules\gulp-cssmin\node_modules\clean-css\lib\selectors\extractor.js:66 return name.replace(/^\-\w+\-/, ...

Struggling to retrieve JSON data from the MercadoLibre API while consistently encountering the CORS error?

I have been attempting to access a mercadolibre API that provides JSON data I need to utilize. However, whenever I make an AJAX GET request, I keep receiving the same error: "Response to preflight request doesn't pass access control check: It does n ...

Is there a way to incorporate borders into JqTree?

I attempted the following .jqtree-tree .jqtree-element { border-color: black; border-width: 10px; } It seems that I may be overlooking certain elements to which this CSS should be applied ...

"Despite successfully clicking on the menu with Selenium WebDriver, no action or change occurs on the webpage

Currently, I am in the process of writing a selenium C# test script for an older "infragistics" webpage, which I must admit, is not something I am very familiar with. The challenge I am facing is that the page does not provide many options for referencing ...

Challenge in Decision Making

I am curious why this type of selection is not functioning properly for html select options, while it works seamlessly for other input types like Radios or checkboxes. Any thoughts? $('#resetlist').click(function() { $('input:select[nam ...

Is there a way to change the name within an array of objects?

let myArray = [ { age: 21 }, { name: 'Sara' }, { test01: 'bla' }, { test02: 'bla' } ]; I have an array of objects and I need to update only the "name" property. How should I go about it? This is the desired outcome: ...

What steps should be taken to address the issue when the .media$thumbnail.url cannot be located

Creating a custom widget for bloggers <script type="text/javascript"> function mycallback(jsonData) { for (var i = 0; i < jsonData.feed.entry.length; i++) { for (var j = 0; j < jsonData.feed.entry[i].link.length; j++) { ...

Determining the sum of items selected from a dropdown menu using Ajax

Is there a way to calculate values from two drop-downs using ajax or php? Sample Form Base Price: $10 Color [drop-down] Size [drop-down] Base Price: $10 <select name="color"> <option>Blue</option> <option>White&l ...

Angular - Automatically create thumbnails for uploaded images and videos

I am working on an Angular application that allows users to upload files. My goal is to create thumbnail images for uploaded images and videos without saving them locally. Instead, I plan to pass them along with the original file data to another API. Howev ...

Styling with CSS: I am looking to display images beneath text

I am facing a challenge with displaying an image within a container. The container has a background image with the position set to relative and contains some text. I would like to display another image within this container, with the position set to absolu ...

Counting the number of key-value pairs for a specific key in a JSON data can be achieved by implementing

Is there a way to determine if __metadata and ItemToEkbeNav are the root elements for their children who have key-value pairs? I've attempted various methods such as Object.keys().length and Array.isArray(), but haven't been able to retrieve the ...

To properly render an HTML file before loading a JavaScript file, express.static fails to serve the HTML file

Within my server.js file, the following code is present: var path = require('path'); var express = require('express'); var app = express(); var htmlRoutes = require('./app/routing/routes.js')(app, path, express); As for my r ...

The delay between initiating a Next JS route by clicking and the corresponding page actually loading

Just launched my website at this link: I am experiencing a delay of up to 2 seconds between clicking the header links and the page loading. The site works fine in development and on localhost, so I'm not sure why there's such a slowdown on the l ...

Missing dots on owl carousel

Currently, I am working on a project that involves using the owlcarousel library in javascript. Everything was running smoothly until I encountered an issue. Although I have set the dots to true in the code, they are not appearing on the carousel. Below i ...

The challenge of multi-level sortable lists in jQuery and Internet Explorer

Greetings to the Stack Overflow community, Unfortunately, I encountered a compatibility issue with Internet Explorer when utilizing the jQuery library along with Ajax. The problem at hand can be observed in my live demonstration: Try sorting the items i ...