Instructions on adding a class to a span within a div upon clicking the div

Is there a way to dynamically add a class to the span element inside a specific div when that div is clicked? There are multiple divs and spans with the same class name.

$('.menu_item_conteiner').click(function() {
  $('.menu_item_conteiner').removeClass("tab_selected");
  $(this).addClass('tab_selected');

  // This adds a class to the last span, not the one inside the clicked div.
  // $(".menu_item_conteiner span:last").addClass("show_element");

});

HTML

<span class="menu_item_conteiner">
   <span class="menu_item">
     Text
  </span>
  <span class="menu_imagen_icon">
  </span>
</span> 

Answer №1

Below is the code needed for this specific task:

$('.menu_item_container').click(function() {
   $('span.menu_item').removeClass('active');
   $(this).find('span.menu_item').addClass('active');
}

Answer №2

Here is my approach:

When the 'menu_item_conteiner' element is clicked, this code will add the class 'otherclass' to the span element:
$('.menu_item_conteiner').click(function() {
   var d = document.getElementById("span");
   d.className = d.className + " otherclass";
}

Alternatively, you can achieve the same result with this code:

Another way to add a class ('some_other_class') to the span element when 'menu_item_conteiner' is clicked:
$('.menu_item_conteiner').click(function() {
   var d = document.getElementById("span");
   d.addClass( 'some_other_class' );
}

Both methods will append the specified class to the targeted span element.

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

Oxygen-style with a sleek, shadow-free frame

Currently, I'm utilizing the qtoxygen style and I am curious if anyone knows how to remove the shadow frame on QPlainTextEdit. Even after attempting to customize the CSS with { border: none }, the shadow continues to be displayed. ...

The implementation of $(this).addClass within AJAX success is not yielding the desired results

Currently, I am working on some ajax processing for a click event. Everything is functioning correctly so far, but now I have encountered an issue when trying to add a class in the success function. I am using $(this).addClass("cover");, however, nothing ...

issues with jquery progress bar not updating value

How can I display two progress bars with the same value specified in the data attribute? Here is the HTML code: <div> <div class="p" data-value="54"></div> </div> <div> <div class="p" data-value="45"></div> < ...

Error message: Iframe chrome encountered a Uncaught DOMException when attempting to access the 'localStorage' property from 'Window': Document does not have permission

I recently developed a JavaScript widget that utilizes localstorage to set and retrieve properties of the window. Upon opening this widget in Chrome, I encountered an error message: Uncaught DOMException: Failed to read the 'localStorage' prop ...

Discovering what is impeding the rendering of a webpage

On a particular website, the server rendering happens quickly but there seems to be a delay on the client side which is causing the HTML to be rendered few seconds later. I suspect it could be due to some setTimeout function or similar; are there any tool ...

Transformation of looks post a refresh

Initially, the CSS and appearance of the page look fine when I first open it (after clearing the cache). However, upon refreshing the page, a part of it changes (specifically, the padding direction of a div). This change occurs consistently with each refre ...

Yeoman webapp error: Issue with incorrect CSS path in subfolder

I have been using the yeoman webapp generator (0.5.0) and my app directory is structured like this: app/ ├── dir1 │ └── index.html ├── favicon.ico ├── images ├── index.html ├── robots.txt ├── scripts │ └ ...

Although JQuery is able to remove a row from the page, the record in the database is not

I'm having some trouble using jQuery and Ajax to delete a row on the page as well as a record in the database. The row on the page is successfully removed, but the PHP file isn't being called. I only have an echo statement in the PHP for error ch ...

What is the best way to activate a click event on a dynamically generated input element with the type of 'file'?

I am facing a challenge in dynamically adding an input field of type 'file' to a form. The requirement is to allow the end user to add multiple files to the form, necessitating the use of an array concept. While I am able to inject dynamic elemen ...

Only initiate the loading of the iframe within the div upon clicking a specific element

Is there a way to delay loading the content of a div until it's clicked, instead of having all data loaded at once when the page loads? I noticed that removing unnecessary divs made the page load much faster. How can I prevent all data from being loa ...

Adjusting the line-height and baseline to accommodate various screen sizes on both mobile and desktop

This problem seems to keep coming back for me. I can't figure out what's causing it. Here are two images showing the issue: On desktops: On mobile devices: As you can see, the text is not vertically centered on mobile devices. Strangely, thi ...

Retrieve the value of an AngularJS expression and display it in a window alert using AngularJS

Hey there, I am in the process of trying to display the value of an expression using AngularJs As a beginner in angular, I am working on figuring out how to retrieve the value of the expression either with an alert or in the console. I'm utilizing A ...

How should we structure our JavaScript code: MVC or self-rendering components?

I'm in the process of developing a highly JS-centric web application. The bulk of the work is being carried out on the client side, with occasional syncing to the server using AJAX and XMPP. This is my first venture into creating something of this ma ...

Ways to stop the floating label from blending with the input field

Attempting to incorporate the Bootstrap Material design theme into my project, I have implemented a basic text input field using the code below: <div class="form-control-wrapper"> <input class="form-control empty" type="text"></input> ...

Try using the doubletap event instead of dblclick in jquery mobile, although it seems to be ineffective on iPad devices

Is there a way to fix the issue with jQuery Mobile doubletap option not working on iPad and instead only zooming the page? Thank you for your help. $("#book div img").live('doubletap', function() { $("#book").turn("disable", true); ...

Transferring information to the controller and refreshing the interface (Single-page design)

I'm stuck on a personal project and could really use some help. If anyone has any feedback, it would be greatly appreciated. Thanks in advance. In my index.php file, I have a div with the id main-container. Inside this container, there are two separa ...

Using jQuery, it is possible to loop through each row in a table by targeting the table element

In jQuery 1.8.2, I have a code snippet that retrieves a table element for a given row using the following script: var tbl = $("input[id$='chkSelectAll']").closest('table'); Is there an equivalent jQuery script to replace this JavaScri ...

Popup to confirm JqGrid inline editing

While I am working on inline editing with jqgrid, the client has requested a confirmation dialog to appear when saving changes, similar to how delete operates. Upon reviewing http://github.com/tonytomov/jqGrid/blob/master/js/grid.inlinedit.js, it is eviden ...

Organize the structure of a JSON object in AngularJS to generate a hierarchical unordered list

Greetings! I am working with a RESTful API that returns a JSON object structured like this: { data: [ { id: 4, name: "Naranja", cant_portion: 2, portion_name: "Piezas", foodgroup_name: "Frutas" } ...

Avoid repeating media queries in MUI JSS by combining them efficiently

Is there a more efficient way to apply the same set of CSS styles to multiple media query rules without repetition? Currently, the only workaround seems to be: [theme.breakpoints.up('xl')]: { color: 'red', backgroundColor: 'gre ...