How can I modify the class attribute in JavaScript when the onclick event is triggered

Hello, I am looking to achieve this functionality using JavaScript only (not jQuery).

How can you accomplish something similar like the code below but using pure JavaScript instead of jQuery?

<body>

<div class="wysiwyg">text......text...</div>

<div class="btn btn-primary verMas">Ver más</div>

<script>
    document.querySelector('.verMas').addEventListener('click', function(e) {
        e.preventDefault();
        document.querySelector('.wysiwyg').style.height = 'auto';
        this.style.display = "none";
        document.querySelector('.layerMiddle').style.display = "none";
    });
</script></body>

Answer №1

In order to achieve the actions you described, it is necessary to:

  1. Identify Items within the Document Object Model (DOM)
  2. Add an event listener for click events
  3. Adjust the visibility of elements on the page

If we were to tackle these tasks using pure JavaScript, here are some methods you could utilize:

  1. document.querySelector()

This function bears resemblance to JQuery's selector methods, specifically in this context.

  1. element.addEventListener('click', function)

This approach mirrors the functionality provided by JQuery's $('').click(function) method.

  1. element.style

Using this enables direct adjustments to an element's inline styles, making it possible to set properties like display: none.

Combining these techniques:

// Use querySelector to find the .verMas element and attach a click event listener
document.querySelector('.verMas').addEventListener('click', e => {

  // Prevent the default behavior of the event
  e.preventDefault()

  // Find the .wysiwyg element and adjust its height parameter to auto
  document.querySelector('.wysiwyg').style.height = 'auto'

  // Hide the clicked element
  e.target.style.display = 'none'

  // Locate and hide the .layerMiddle element
  document.querySelector('.layerMiddle').style.display = 'none'
})
div {
  padding: 10px;
}

.btn{
    display: inline-block;
    padding: 6px 12px;
    text-align: center;
    white-space: nowrap;
    vertical-align: middle;
    cursor: pointer;
    border: 1px solid transparent;
    border-radius: 4px;
    color: #333;
    background-color: #fff;
    border-color: #ccc;
}
<div class="wysiwyg">text......text...</div>
<div class="layerMiddle">[Layer Middle]</div>
<div class="btn btn-primary verMas">Ver más</div>

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

Ways to customize the appearance of Bootstrap 4 tooltips

I'm having trouble customizing my Bootstrap 4 tooltip. I want to adjust the background color, height, width, and border, but I can't figure out how to style it to my liking. Here is the HTML code I am using: <i class="fa fa-question-circle" ...

After the ajax call has finished loading the page, I would like to toggle a particular div

When I trigger a click event on an element, it calls a function with Ajax calls. How can I toggle a div (which will be loaded after the Ajax call) once the page is loaded? function triggerFunction(){ $("#trigger_div").trigger("click"); $("#to ...

What is the best way to separate text entered by the user into its individual characters?

Is there a way to allow users to drag each character they type on the screen individually? I'm trying to figure out how to access and manipulate each character the user enters. In the example below, I can manipulate each character of the word "place ...

Using the variable obtained from a successful ajax call and inserting it into a jQuery selector

I'm encountering an issue with an ajax function that includes a success block. Within this block, I am attempting to remove an element. Here is the content of the success block: success:function(data, textStatus, jqXHR) { $("#comment +$data['p ...

Determine the corresponding Y value for a given X value

After creating this chart (refer to the screenshot), I am now seeking a method to extract the Y value corresponding to a specific X value. I tried the solution mentioned here, but it only yielded a value of 1.3685770182056411e-13 Since I am utilizing D3 ...

What is the best way to make my a tag background appear above my div background?

Beginner question here. I am experimenting with z-index, but it doesn't seem to be functioning as expected. Below is the code snippet I am working with: <a id="favoritelink" href="#" style="margin-left: 600px" class="addtofavorites" title="Add to ...

Using the class for jQuery validation as opposed to the name attribute

I am looking to implement form validation using the jquery validate plugin, but I am facing an issue with using the 'name' attribute in the html since it is also used by the server application. Specifically, I want to restrict the number of check ...

Replace Picture as you Scroll

Is it possible to swap images based on scroll position? For instance: When scrolling 100px, display img1.jpg When scrolling 200px, display img2.jpg When scrolling 300px, display img3.jpg Here's an example of what I'm looking for. Any suggest ...

Determine the number of elements chosen within a complex JSON structure

I need to figure out how to count the length of a jsonArray, but I'm stuck. Here's an example to start with: https://jsfiddle.net/vuqcopm7/13/ In summary, if you click on an item in the list, such as "asd1", it will create an input text every t ...

Ways to conceal the header and footer on specific React pages

I currently have my header and footer wrapping the content on all pages of my website. However, I am looking to hide the header and footer on specific pages like the customer and admin dashboard. Can anyone suggest the best approach to achieve this? cons ...

Encountering the "excessive re-renders" issue when transferring data through React Context

React Context i18n Implementation i18n .use(initReactI18next) // passes i18n down to react-i18next .init({ resources: { en: { translation: translationsEn }, bn: { translation: translationsBn }, }, lng: "bn ...

Utilizing a self-invoking function to incorporate JavaScript

In my role, I am responsible for managing a Content Management System (CMS) used by developers to create content that involves JavaScript. In the past, we placed the content in an iFrame for containment purposes; now, it operates as a single-page client-si ...

Silly problem arising from the animate feature in jQuery

Apologies for my poor English. I am facing an issue with the animate function of jQuery in my code snippet. It seems to work fine at line 2, but it doesn't work at line 3. Can someone help me understand why? $('.opac').hover(function(){ ...

Centering an Image of Unspecified Dimensions Both Vertically and Horizontally with Hover Effect on Anchor

To achieve both horizontal and vertical centering of an image with unknown dimensions, I need to use max-width and max-height to constrain it within a container size of 101x84. Additionally, I want the image to display an icon in the middle when hovered ov ...

Identifying the moment when the body scroll reaches the top or bottom of an element

I have been experimenting with javascript and jquery to determine when the window scroll reaches the top of a specific element. Although I have been trying different methods, I have yet to see any successful outcomes: fiddle: https://jsfiddle.net/jzhang17 ...

A guide to implementing Quasar Framework and/or Vue3 using Bun.js

After using the bun create [..] command to easily create a react and a next project by following the instructions on the bun git repository (note: additional instructions are available at bun.sh), I encountered a problem with setting up quasar/vue. While ...

The blackberry device struggles to load the mobile website

Our mobile website solution is powered by the following technologies: Asp.net mvc 4 (running in .net 4) Jquery Mobile (v1.3.0) Jquery validation Is it possible that any of these technologies are incompatible with Blackberry's native browser? We co ...

Gulp Watch fails to identify changes in the SASS SCSS directory

After setting up Gulp to compile SCSS into CSS using NanoCSS and gulp-css for the first time, I encountered an issue. While my do-sass command successfully compiles SCSS and minifies CSS files, it does not run when placed within a watch task. Any changes ...

Tips for triggering both server side and client side events in ASP web forms

In an ASP.NET web form, I have the following code snippet: <asp:UpdatePanel ID="udpNames" runat="server"> <ContentTemplate> <div class="expanderheaders"> <asp:Image ID="epImgNames" ...

The left margin in Carousel is malfunctioning when it comes to hovering

I would like to achieve an effect where, on hover, my images shift the other images to both the left and right. However, currently when hovered, all the images shift to the right only. The code snippet in question is as follows: .item-img:hover { trans ...