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'; }); });
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');
Comments
Post a Comment