4

I have span element which I wish to toggle the contents of, between the words 'Price' and 'Net', onclick of another element.

Essentially, it needs to test the existing cell contents to see which is currently present and then swap it with the other.

Something like this:

<input
    type    = "checkbox"
    name    = "element1"
    onclick = "$(#element2).toggleHTML('Price', 'Net')">

<span id="element2">Price</span>

I made up the toggleHTML method used above to demonstrate how I expect it might work.

2

5 Answers 5

8

You can use html or text method's callback function.

$('input[name="element1"]').click(function() {
    $('#element2').text(function(_, text) {
        return text === 'Price' ? 'Net' : 'Price';
    });
});

http://jsfiddle.net/BLPQJ/

You can also define a simple method:

$.fn.toggleHTML = function(a, b) {
    return this.html(function(_, html) {
        return $.trim(html) === a ? b : a;
    });
}

Usage:

$('#element2').toggleHTML('Price', 'Net');

http://jsfiddle.net/AyFvm/

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

2 Comments

Thanks. This is the code I used. Although there are several answers here which are similar, you put a little more effort in than most, by providing multiple solutions & jsfiddle. Thanks and thanks also to everyone else.
Great little utility function, nice one. What is the function(-,html) about though ?
1

You can do something like this :

var html = $('#element2').html();
html = html.indexOf('Price') > -1 ? "Net" : "Price"; 
$('#element2').html(html)

Comments

1

try this

HTML

<input
type    = "checkbox"
name    = "element1"
id="input1">

JQUERY

$('#input1').click(function(){
    $('#element2').text(function(){
      return $('#element2').text()=="Price" ?"Net":"Price" ; 
    });
}); 

avoid using inline javascript ...

fiddle here

3 Comments

Thanks. I'm wondering why it's best to avoid using inline javascript. I'm not nit-picking, just looking to increase my understanding. Thanks.
its not necessary actually.. it depends..avoiding makes the code cleaner and lot more easier to read if you have lots of codes in there..anyways here you go... programmers.stackexchange.com/questions/86589/…...
Thanks for the link. Very informative (and the rest of the site too).
0

this is the code : html :

<input type = "checkbox" name = "element1" id="element1">

Javascript :

$(document).ready(function()
{
    $("#element1").click(function()
    {
        if($("#element2").html() == "Price")
            $("#element2").html("Net");
        else
            $("#element2").html("Price");
    });
});

Comments

0

I have a similar issue, My page has multiple 'read more' toggles which suppose to change to 'read less' and back again on each click. but its only worked for the first iteration. the others only toggle once then stop, but the link works fine for the first one only.

code:

$(".morelink_L3").click(function() {
  $(this).find("span").html($(".morelink_L3 span").html() == 'Read less' ? 'Read more' : 'Read less');
});

html:

<span class="moreLink morelink_L3" value="True">
     <span>Read more</span>
</span>

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.