1

I want to click a button (or in my example below, a link) and toggle between 2 blocks of content to essentially go from displaying content to editing content.

I have the following snipits of code:

<style type="text/css">
    div.show {display:block;}
    input.hide {display:none;}
</style>

<a href="#">edit</a> <!--not sure what my jquery should look like to do toggling-->
<div class="show" id="d-01-on">Some content</div>
<input class="hide" id="d-01-off" name="d-01" value="Some content" />

Thanks for helping.

[see How to Streamline and Compress Repetitive jQuery Code for expansion to this techique]

3 Answers 3

1

It is easy with jQuery just add the following JS code:

$(document).ready(function(){
   $('#button').on('click', function() {
        $('#d-01-on').toggle();
        $('#d-01-off').toggle();
   });
})

And it will work with your current markup and css code.

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

1 Comment

if you have a moment @Skatox could you take a look at my follow-on problem at stackoverflow.com/questions/13159735/… ? Thanks.
1

to show and hide an element using jquery is done as follows:

HTML:

<div id="d-01">content</div>

Javascript:

$.("#d-01").toggle(); //toggles display so if shown then hides, if hidden then shows;
$.("#d-01").show();  //shows div
$.("#d-01").hide();  //hides div

3 Comments

also, to trigger this from a hyperlink, wrap the jquery code in a function definition, then give your <a> tag an onclick property with the function's name as its argument
Cool, thanks @mr_lewjam...giving it a try now. I'll post back in a few moments.
Thanks @mr_lewjam. I worked through this until I saw Skatox's suggestion below. I then tried that one and got it working really quick. So I am going to mark Skatox as the answer, but I really appreciate your suggestion and I am sure I could get it to work with more effort.
1

This will toggle visibility of both elements:

function handleClick() {
  $('div.show').toggle();
  $('input.hide').toggle();
}

Then of course your class names will be wrong. You could use IDs instead.

1 Comment

Thank you for the suggestion @ChrisMiller. I am going with Skatox below as I got it running really quickly.

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.