1

I'm trying to create a flexible toggle function with jQuery where I take the ID of the item to be toggled as a parameter. the function is triggered by a link which passes the correct ID to the function.

here is the code at the moment:

function boldToggler(itemid){
$(itemid).toggle(function(){
    $(itemid).css("font-weight","bold");},
function(){
    $(itemid).css("font-weight","normal");});
}

which is triggered by links like this:

<a href="javascript: boldToggler('h1')">toggle style for this item</a>
<a href="javascript: boldToggler('h2')">toggle style for this item</a>

is there a way to get this to work? it would save me writing hundreds of toggle functions!

Any help would be amazing, thanks

1
  • 1
    the toggle function is deprecated in the latest version of jquery Commented Sep 5, 2012 at 12:04

7 Answers 7

5

Do you mean something like that?

function boldToggler(itemid) {
    $(itemid).css("font-weight", function(i, val) {
        return val == "bold" ? "normal" : "bold";
    });
}

DEMO: http://jsfiddle.net/kbQXd/

Sign up to request clarification or add additional context in comments.

5 Comments

I'll upvote this one, as using classes or the deprecated toggle() function is'nt really an answer.
Your example only works to bold the text. Not working for returning it to normal. I changed the return statement to return val == 700 ? 400 : 700; and it worked, fiddle : jsfiddle.net/kbQXd/1
@VisioN : Doesn't work for me. May be some issue at my end. +1 anyways :)
Yes this is great! lets me pass tags or id's. thank you - if you have a minute, I was wondering what the i and val are passing exactly? would love to learn this, dynamic parameter lists/passing events etc! thanks again! joe
@Joe This is a handler that receives 2 arguments: index of element and the old value. In order to change the value, the function should return new value. You can read about it here: api.jquery.com/css/#css2.
2

Maybe this alternative code may be useful - Fiddle: http://jsfiddle.net/9zxbT/

HTML

<a href="#" data-toggle="h1">toggle style for H1</a>
<a href="#" data-toggle="h2">toggle style for H2</a>

<h1>this is H1</h1>
<h2>this is H2</h2>

CSS

.bold { font-weight: bold; }

jQuery

$('a[data-toggle]').on('click', function(evt) {
    evt.preventDefault();
    $($(this).data('toggle')).toggleClass('bold');
});

this completely removes your inline handlers, performs a better separation of javascript and CSS and make use of data-* attributes to set the desired selector

2 Comments

I swear I did not steal your idea... looks like great minds think alike!
Hi guys, nice code and thanks for the advice- for my particular application I need to edit the html directly without using extra classes (its for a cms template editor) - i will heed your advice on the data types in future though! thanks, joe
1

change your function to this

function boldToggler(itemid){
$('#' + itemid).toggle(function(){
    $('#' + itemid).css("font-weight","bold");},
function(){
    $('#' + itemid).css("font-weight","normal");});
}

Comments

1

Id be tempted to change your markup a bit to make it a bit more jQuery-ish:

<a href="#" class="boldToggler" data-selector="h1">toggle style for h1</a>
<a href="#" class="boldToggler" data-selector="h2">toggle style for h2</a>

Then use a method like:

$('.boldToggler').click(function(){
    var itemSelector = $(this).data('selector');
    $(itemSelector).toggleClass('bold');
});

With a css class defined as:

.bold{
 font-weight:bold;  
}

Live example: http://jsfiddle.net/NTSzs/

2 Comments

as in my solution you can avoid the class and simply use a[data-selector]
@FabrizioCalderan - Yeah, totally agree. I'll leave this as-is though to demonstrate a slightly different way of approaching the issue. After all, he might want an italicToggler too using the same data-selector attribute but changing a different cssClass!
0

you're missing the '#':

$('#'+itemid).css....

Comments

0

You can use the toggleClass method in jQuery.

<a href="javascript: $('h2').toggleClass(bold);">toggle style for this item</a>

You just need to define the class:

.bold { font-weight: bold; }

Comments

0
function boldToggler(itemid){
    if($('#'+itemid).css("font-weight") == "normal"){
        $('#'+itemid).css("font-weight",'bold');
    }else{
        $('#'+itemid).css("font-weight",'normal');
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.