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.
ready(function() { $('#search').
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.
If you want native JS to trigger click event without clicking then use the element id and click() method of JavaScript.
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.
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.
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" });
}
);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With