Skip to content

Instantly share code, notes, and snippets.

@matticustard
Created February 12, 2019 23:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matticustard/f3be8349d443365e8025ceb3f2cf52a1 to your computer and use it in GitHub Desktop.
Save matticustard/f3be8349d443365e8025ceb3f2cf52a1 to your computer and use it in GitHub Desktop.
Simple example: Show or hide secondary paragraph by clicking a link (requires jQuery).
<p>
Your visible text.
</p>
<a id="toggle_text" href="#!">More...</a>
<p id="hidden_text" style="display: none;">
Your hidden text.
</p>
<script>
// wait for document to be fully rendered
$('document').ready(function() {
// capture click event
$('body').on('click','#toggle_text',function(e) {
console.log('ok');
// prevent default link clicking action
e.preventDefault();
// define the target object you want to control
var target = $('#hidden_text');
var link = $('#toggle_text');
// toggle the visibility and link text
if (target.is(":visible")) {
// hides target and sets link text to "More..."
target.hide();
link.html('More...');
} else {
// shows target and sets link text to "Hide..."
target.show();
link.html('Hide...');
}
});
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment