Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the opposite of CLICK event?

I had a list <li> where the content inside li should get bold when it is clicked. For that i used the following code

HTML

  <ul class="tabs">
        <li><a href="#tab1" style="padding-left:5px;">All Sectors</a></li>
        <li><a href="#tab2">Information Technology</a></li>
        <li><a href="#tab3">Manufacturing</a></li>
        <li style="border-right:none;"><a href="#tab4">Services</a></li>        
  </ul>

JQUERY

$(document).ready(function() {
    $(".tabs li a").click(
        function() { $(this).css({ "font-weight" : "bold" }); }     

    );

});

But When the list item is clicked it gets bold. I want the list item to get back to normal when the other list item is clicked. I am not able to find the correct event. Your help will be greatly appreciated.

like image 890
Rajasekar Avatar asked Jun 27 '10 12:06

Rajasekar


People also ask

What is the opposite of onclick in HTML?

ready(function() { $('#search').

What does click event mean?

An element receives a click event when a pointing device button (such as a mouse's primary mouse button) is both pressed and released while the pointer is located inside the element.

How do I trigger a click event without clicking?

If you want native JS to trigger click event without clicking then use the element id and click() method of JavaScript.

How do you know if an event is clicking?

To check if an element was clicked, add a click event listener to the element, e.g. button. addEventListener('click', function handleClick() {}) . The click event is dispatched every time the element is clicked. Here is the HTML for the examples in this article.


2 Answers

It would actually be easier to do this at the <li> level rather than the <a> since it would have the same bolding effect (and direct access to .siblings()), like this:

$(".tabs li").click(function() {
  $(this).addClass("boldClass").siblings().removeClass("boldClass");
});

Then you can use CSS for the class like this:

.boldClass { font-weight: bold; }

Instead of .addClass("boldClass") you could use .toggleClass("boldClass") if you want a click on an already-bold link to un-bold it.

like image 97
Nick Craver Avatar answered Sep 24 '22 12:09

Nick Craver


Maybe set the other items to normal before setting the clicked item to bold? I'm not the greatest with jQuery so this code my be utter crap :-)

$(document).ready(function() {
    $(".tabs li a").click(
        function() { 
           $(".tabs li a").css({ "font-weight" : "normal" }); 
           $(this).css({ "font-weight" : "bold" }); 
        }     
    );
});
like image 39
amarsuperstar Avatar answered Sep 25 '22 12:09

amarsuperstar