How can I convert the hover action into a click action?

Greetings everyone! I have created two divs and want to display something only when the h3 is clicked, not hovered over. How can I achieve this? I tried changing hover to click but it didn't work as expected. Apologies for any confusion in my explanation.

$(document).ready(function () {
    $('li.requirement').click(function () {
        $(this).find('span').toggle();
    });
});
#wrap {
    background: #e7e7e7;
    padding: 0px; 
    text-align: center;
    width: 100%;  
}

#left, #right {
     background: #ccc;
     display: inline-block;    
     padding: 20px;   
}


li {
    list-style-type: none;
    padding: 0px;
    margin: 0px;
}
    
span.lewy {float:right; background:red; padding:20px;}
span.prawy {float:left; background:red; padding:20px;}

h3 {text-align:center;}

h3.praw {float:left;}
h3.lew {float:right;}

.calosc {max-width:500px; margin: 0 auto; border:1px solid red;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="wrap">
    <div id="left"><div class="lef">

<li class="requirement" id="requirement_1">
     <h3 class="lew"><a href="#">SPR</a></h3>
 <span class="fr drag lewy" style="display:none;">1 kontakt</span>
</li>

</div></div>
    <div id="right"><div class="praf">

<li class="requirement" id="requirement_2">
     <h3 class="praw"><a href="#">SPR 2</a></h3>
 <span class="fr drag prawy" style="display:none;">2 kontakt</span>

</li>

</div></div>
</div>

Answer №1

To make an element clickable with jQuery, you can use the method .on('click', function(){});. Within this function, you can check if the element is visible or not before taking any action. Here's an example:

UPDATE
If you want only the <h3> tag to be clickable, you need to modify the code accordingly. You now have to check the visibility of the parent of the h3 element, as the context of this has changed from li to h3.

$(document).ready(function () {
    $('.clickableH3').on('click', function () {  
        if ($(this.parentElement).find('span').is(":visible")){
          $(this.parentElement).find('span').hide();
        }else{
          $(this.parentElement).find('span').show();   
        }
    });
});
#wrap {
    background: #e7e7e7;
    padding: 0px; 
    text-align: center;
    width: 100%;  
}

#left, #right {
     background: #ccc;
     display: inline-block;    
     padding: 20px;   
}


li {
    list-style-type: none;
    padding: 0px;
    margin: 0px;
}

span.lewy {float:right; background:red; padding:20px;}
span.prawy {float:left; background:red; padding:20px;}

h3 {text-align:center;}

h3.praw {float:left;}
h3.lew {float:right;}

.calosc {max-width:500px; margin: 0 auto; border:1px solid red;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="wrap">
    <div id="left"><div class="lef">

<li class="requirement" id="requirement_1">
     <h3 class="lew clickableH3"><a href="#">SPR</a></h3>
 <span class="fr drag lewy" style="display:none;">1 kontakt</span>
</li>

</div></div>
    <div id="right"><div class="praf">

<li class="requirement" id="requirement_2">
     <h3 class="praw clickableH3"><a href="#">SPR 2</a></h3>
 <span class="fr drag prawy" style="display:none;">2 kontakt</span>

</li>

</div></div>
</div>

Answer №2

As you may have noticed, in the JavaScript code, if you replace "hover" with "click", the functionality will change...

Answer №3

The jQuery hover function allows for 2 parameters, as seen in your case - one for hovering and the other for unhovering.

If you want to close and hide on click, I recommend using some CSS and toggleClass. However, if you prefer to stick with just JavaScript, you can achieve it like this:

$(document).ready(function () {
    $('li.requirement').click(function () {
        var $elm = $(this);
        if( $elm.hasClass('showed') ){
            $elm.find('span').removeClass('showed').hide();
        } else {
            $elm.find('span').addClass('showed').show();
        }
    });
});

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

Utilizing Ionic to import and parse an Excel file for data processing

I need assistance in uploading an Excel file and reading it using Ionic-Angular. I tried the following code, but it only seems to work for browsers and not for the Ionic app on Android. Can someone please help me with this issue? I am trying to read the E ...

Maximize the use of block level buttons on the navbar menu for small screens, while maintaining the regular size buttons when the menu is expanded in

Currently utilizing bootstrap 4 beta. In my navbar, standard sized buttons are used as nav-items when the screen is full-sized. These buttons align to the right of the navbar next to the site logo positioned on the left side of the navbar. https://i.ssta ...

The action 'onDeleteClick' cannot be executed as the property is undefined

Why is the onDeleteClick function working on the first <div>, but returning undefined on the second one? I am able to access the reference of this, but when clicking, it shows "onDeleteClick of undefined". I am confused as to why the onDeleteClick f ...

Utilizing jQuery's AJAX GET method with custom parameters to retrieve webpage content

Seeking assistance with using jQuery's get function to call a php script. The php script returns a variable containing the main content of my page, excluding the header/footer. The goal is to update the page content without a reload. Any insights on ...

Flexbox causing spacing problems in HTML/CSS navigation bar

I have encountered a unique issue that I cannot seem to find a solution for, so here goes: I am in the process of designing a navigation bar with the heading positioned on the left-hand side and the navigation items on the right. My goal is to center the ...

Struggling to connect HTML elements with AngularJS and TinyMCE?

My objective is to edit text using tinymce, persist it in a database, and display it within a div by incorporating angularJS for the same styling and formatting. I am utilizing tinymce 3.5.8 with an angularUI directive. I have successfully saved the wysiw ...

Error message: An error occurred while executing the AJAX PHP code due to a TypeError, specifically stating that the property 'status' cannot be

For some reason, I keep receiving an undefined return for my response. The event handler in index.php triggers the following code: $.post("getData.php", onNewPost()); function onNewPost (response){ if (response.status == "OK") { console.log(resp ...

When developing a Discord bot, watch out for the BitFieldInvalid error, which occurs when an invalid bitfield flag or number is

Recently came across this code in a forum post and decided to try using it to create my first bot. I want the bot to send an @everyone announcement when someone joins a voice channel. However, I'm struggling with an error message that says "RangeError ...

Having issues with utilizing $fetchState in Nuxt 2.12

Recently, I've been exploring the new functionality outlined in the documentation. However, I'm encountering an error that states : Property or method "$fetchState" is not defined on the instance but referenced during render. Despite clearly ...

Methods for sending an indexed array object back to a getJSON request with jquery

When sending an Indexed Array to a PhP file on the server side using getJSON, it seems like using an associated array may be a better choice. However, the indexed array works fine and the data is successfully received on the server. I am able to process th ...

Fluctuating between different currencies

I have been trying to tackle this issue for some time now - how can I set up the functionality to switch between currencies seamlessly? The first select dropdown should include options for EUR, USD, and GBP, while the second should offer UAH, GBP, and EUR. ...

Issue with uploading an Excel file via REST: The POI library refuses the file due to a problem with the

I am facing an issue while attempting to upload an excel (.xlsx) file using AngularJS and REST (Jersey). The multipart boundary is being added to the file received in REST, causing the apache POI Library to reject it as an invalid file with the following e ...

Is it possible to utilize the text-search feature in browsers for an HTML5 application?

Our team is currently developing a new feature for our HTML5 app that mimics the 'text search' function found in web browsers, which highlights specific texts. We're considering whether we can utilize this existing feature in browsers by ca ...

Display a unique error page using jQuery when the URL is incorrect

I am currently working on a web application built with jQuery and HTML. If a user enters an incorrect URL, like www.abcd.com/menupa instead of www.abcd.com/menu, I want to redirect them to my custom error page. I have searched online for a solution but h ...

Exporting inline SVG objects (Troubleshooting Internet Explorer)

Issue at Hand: I am attempting to extract an SVG Inline Object using JavaScript and transmit it to the Server in a way that accurately represents the provided SVG Object. Everything functions smoothly in Chrome and FF, however, IE (version 9 or higher) t ...

Avoid the issue of markers clustering in Leaflet to have distinct markerClusterGroup icons without overlap

Is there a way to prevent two separate markerClusterGroups from overlapping in Leaflet? I have one with a custom Icon to differentiate between the two clusters, but for simplicity's sake, I've omitted that part in this example. var map = L.map(&q ...

Provide necessary CSS styles exclusively

Is there a method to streamline extensive CSS files by isolating only the necessary selectors for a specific page, and generating new css files containing just these selectors? Scenario: I am dealing with a significantly large CSS file that I need to opti ...

The vertical height scrollbar will automatically show up when a margin-top is added

My HTML: div.container My CSS: html, body { height: 100%; background-color: red; padding-left: 0px; padding-right: 0px; } .container { height: 100%; background-color: blue; margin-top: 5px; *zoom: 1; } I am looking to achieve a spec ...

Develop interactive web applications using Typescript

Having difficulty compiling and executing the project correctly in the browser. The "master" branch works fine, but I'm currently working on the "develop" branch. It's a basic web project with one HTML file loading one TS/JS file that includes i ...

implement an angular directive to apply a CSS element

I am utilizing AngularJS and ng-repeat to populate a dynamic list of studies. This list has the capability to toggle into child elements of each item, creating an accordion-style toggle list that can go up to three levels deep for each list item. I am curr ...