Choosing containers as found in the Firefox debugging tool

I'm currently working on developing a container selector similar to the one found in Firefox's debug tools (press ctrlshift+C). The main goal is to retrieve a list of selected elements for further manipulation.

Here is my current progress:

http://jsfiddle.net/jEHBU/

$('ul,li,a').on('mouseenter', function (e) {
    e.stopPropagation();
    $(this).css({
        outline: "1px solid red"
    });
}).on('mouseleave', function (e) {
    e.stopPropagation();
    if (!$(this).hasClass('box-selected')) {
        $(this).css({
            outline: 0
        });
    }
}).on('click', function (e) {
    e.stopPropagation();
    if ($(this).hasClass('box-selected')) {
        $(this.removeClass('box-selected'))
            .css({
            outline: 0
        });
        e.preventDefault();
    } else {
        $(this).addClass('box-selected').
        css({
            outline: '1px solid green'
        });
        e.preventDefault()
    }
});

This approach works well in simple cases, however, there are issues with event bubbling and the user interface for selection can be improved.

How does Firefox handle this functionality? Perhaps better styling or tooltips could enhance the user experience?

Answer №1

The issue is that the bubbling effect is not working as expected. This occurs because when you hover over an anchor, the ul and li elements are also triggered (sometimes). One solution is to reset all parent elements when hovering over a specific element.

$('ul,li,a').on('mouseenter', function (e) {
    e.stopPropagation();
    $(this).css({
        outline: "1px solid red"
    });
    $(this).parents().each(function() {
        $(this).css({
            outline: 0
        });
    });
});

To improve readability, consider assigning a class instead of constantly adding/removing the 'outline' property each time.

Check out this updated fiddle for reference: http://jsfiddle.net/jEHBU/8/

EDIT:
I made a slight modification to the fiddle linked above to ensure that parent elements do not lose their outlines if they were highlighted by clicking on them.

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

transferring a LatLng variable from one function to initialize Google Maps

I have a database in firebase that stores latitude and longitude values which I retrieve as the variable coords. function getCoords() { var place_data= firebase.database().ref("/place/name"); place_data.once('value').then(function(snaps ...

What is the best way to ensure a footer always remains at the bottom of the page with Telerik controls?

I'm currently working on an asp.net MVC project that utilizes the latest version of Telerik controls. The layout consists of a shared view with a header at the top, a left-side menu, and a footer at the bottom. In the main section, I use a @RenderBody ...

How to set DIVS to be hidden when the page first loads

I am currently working on a project where I need to use JavaScript to hide certain <div> elements when the page loads. I have also included jQuery in my code. Below is what I have tried so far: $(document).ready(function() { hide(); function hid ...

Issue with checkbox alignment in Twitter Bootstrap v3.x

Greetings fellow siblings. I am facing an issue with Twitter Bootstrap checkboxes, particularly when viewed in Opera, Chrome, and IE. Here is the problem: I have come across this snippet of code <div class="form-group"> ...

Trigger a Tabulator event when a checkbox is selected in Vue 3

Currently, I am utilizing Vue3 along with Tabulator (5.2.7) to create a data table using the Composition API. In the following code snippets, I have excluded irrelevant parts of the code. //DataTable.vue <script setup> import { TabulatorFull as Tabu ...

A computed property declared inside a Vue component definition

I'm currently diving into Vue.js 2 and I have a goal of crafting a unique custom component in the form of <bs-container fluid="true"></bs-container>. My intention is for Vue.component() to seamlessly handle the bootstrap 3 container classe ...

How can I align the menu to the right in the navbar using Bootstrap 4?

I'm currently customizing the Bootstrap 4 default navbar and I want to add a collapsing menu that is positioned on the right, instead of the left, when it's not collapsed. So far, I have attempted to use text-right and float-right, but unfortuna ...

Create a hexagon shape using a Div element and start from one of its sides

Is there a way to create a Div with a background image that appears like a hexagon when viewed from one side? Check out this demo to see exactly what I'm looking for: Demo In the example/demo, the header is designed to look like a hexagon from its ...

Tips for managing errors when using .listen() in Express with Typescript

Currently in the process of transitioning my project to use Typescript. Previously, my code for launching Express in Node looked like this: server.listen(port, (error) => { if (error) throw error; console.info(`Ready on port ${port}`); }); However ...

Is this loader going through a triple loop?

<style> .loader { position: fixed; left: 0px; top: 0px; width: 100%; height: 100%; z-index: 9999; background-repeat: no-repeat; background: url('images/page-loader.gif'); } </style> <script src="//ajax.googleapis.com/ajax/libs/jque ...

Is it possible to incorporate an element using absolute positioning using jQuery or Javascript?

When trying to add a new element on the screen, I am facing an issue with the absolute positioning not working properly. Sample Code in Javascript function drawElement(e,name){ $("#canvas").append("<div id='" + name + "' class='e ...

Why does the value of a variable not print when using setTimeout in a loop?

function x(){ for(var i=1;i<=5;i++){ setTimeout(function (i){ console.log(i) },i*1000) } } x(); I'm currently facing an issue with my code where instead of printing the variable i, it's showing "undefined". ...

Create a random number within a specified range using a different number in JavaScript

I am looking for a unique function that can generate a number within a specified range, based on a provided number. An example of the function would be: function getNumber(minimum, maximum, number) { ... } If I were to input: getNumber(0, 3, 12837623); ...

The integration of jQuery in PHP is experiencing some technical challenges

There's one issue driving me crazy: I created a left-floating file tree view and included it in my website. However, when I include my website in the file treeview, it becomes unclickable in Chrome, and in IE 9, the thumbnail part for viewing PDF docu ...

The reactivity of VUE3's data is limited

I am encountering an issue with a basic HTML element that has an @click event attached to it. When I click on the a tag, the value does not update in the VUE console. However, when I check the Google Chrome Inspect Element Console, the idClient value is di ...

Currently, my main focus is on finding a solution to adjusting the background color of the div element named *topnav* to properly match the width of the

I'm having trouble adjusting the width of the topnav background color to fill the entire page without any gaps. I hope that makes sense. Let me know if you need more information. Page displaying gap: Here is the HTML and CSS code: body {background ...

A guide on restricting overflow in React Spring Parallax 'pages'

Currently, I have integrated React Spring Parallax () into my project found at this link: https://codesandbox.io/s/parallax-sticky-scroll-2zd58?file=/src/App.js Upon clicking the button to navigate to the next section, it is noticeable that the image over ...

How come Google Code Beautifier suddenly ceases to function properly when combined with JQuery DatePicker?

When the Google Code Prettifier or JQuery Syntax Highlighter is used on the same page as the JQuery DatePicker, both functionalities may not work correctly. What could be causing this issue? ...

Display the table once the radio button has been selected

Before we proceed, please take a look at the following two images: image 1 image 2 I have over 20 fields similar to 'Image 1'. If "Yes" is selected, then a table like in 'Image 2' should be displayed. This means I have 20 Yes/No fields ...

Instructions on keeping a numerical counter at its current value for all site visitors

Recently, I integrated a number counter into my website. However, I am facing an issue where the count resets to zero whenever a new visitor accesses the site. I'd like the count to remain persistent and update based on the previous count. For instanc ...