html - How to toggle innerHTML between 2 alternative strings using Jquery -


i have span element wish toggle contents of, between words 'price' , 'net', onclick of element.

essentially, needs test existing cell contents see present , swap other.

something this:

<input     type    = "checkbox"     name    = "element1"     onclick = "$(#element2).togglehtml('price', 'net')">  <span id="element2">price</span> 

i made togglehtml method used above demonstrate how expect might work.

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 define simple method:

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

usage:

$('#element2').togglehtml('price', 'net'); 

http://jsfiddle.net/ayfvm/


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -