Click here to Skip to main content
15,891,375 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
XML
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <link href="Css/MyClass.css" rel="stylesheet" />
    <script src="Js/ProductionFile.js"></script>
    <script src="Js/RemoveClass_56.js"></script>
</head>
<body>
    <p><input type="button" value="Toggling of classes" id="btnToggle" /></p>
    <span id="someText">This is some text</span>
</body>
</html>




$(document).ready(function () {
$('#btnToggle').click(function () {
$('#someText').toggle(function () {
$('#someText').addClass('bold');
}, function () {
$('#someText').removeClass('bold');
});
});
});

i am unable to achive my functiolity , insted the span gets hide and show on button click event ..please suggest
Posted

1 solution

Just write the code accurately; and you will achieve the desired effect. Here is the hint: either use toggleClass along or addClass/removeClass without toggle.

Read more carefully:
http://api.jquery.com/toggleClass/[^],
http://api.jquery.com/addClass/[^],
http://api.jquery.com/removeClass/[^].

The function .toogle() is unrelated to CSS classes; it just displays and hides the element:
http://api.jquery.com/toggle[^].

Now think about it: if some content is hidden, what's the use of changing its CSS class? The opposite makes sense: you can change the class, and the CSS classes may have different visibility. But what you are trying to do makes no sense.

—SA
 
Share this answer
 
Comments
Brian A Stephens 18-Jun-14 16:21pm    
It looks like Torakami was trying to use the deprecated toggle method (removed in jQuery 1.9) that allowed for alternating event handlers on an element (http://api.jquery.com/toggle-event/). So in this case, he wasn't trying to hide and show the elements.
Torakami 19-Jun-14 1:21am    
ohh ... yeh that might be the case .. thanks for updating me

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900