Why won't this work?
HTML
<a href="#" id="showMore">Before</a>
JS
$('#showMore').click(function() {
$(this).toggle(
function () {
$(this).html('<a href="#">After</a>');},
function () {
$(this).html('<a href="#">Before</a>');
});
});
Why won't this work?
HTML
<a href="#" id="showMore">Before</a>
JS
$('#showMore').click(function() {
$(this).toggle(
function () {
$(this).html('<a href="#">After</a>');},
function () {
$(this).html('<a href="#">Before</a>');
});
});
Use as:
You are binding a
click
to the<a/>
and than inside of that you are binding atoggle()
to it.If you simply want to toggle you can leave off the
click()
.With that being said you are placing an
<a/>
inside of another<a/>
Is that really what you want? Or you you just want to replace the.text()
?.text("Before")
Jsfiddle example of
toggle()
Blender has the appropriate answer, but we can also generalize your question into a simple jQuery plugin:
Your code would then look like:
because you are nesting a
<a>
inside another, resulting in something like<a href="#" id="#showMore"><a href="">After</a></a>
You should just use
$(this).text('After')
and same with BeforeI guess you wanted to use
replaceWith()
which is not very efficient in this situation because only the text changes.++ the toggle bug spotted in the other answers. Kudos to them ;-)
This is a reduced / "more compact" version of the same answer here: "A simple solution for toggling after a click".
Of course this needs jQuery plugin for it to work.
A simple solution for toggling after a click: