I am having the div like below..
<div id="div1">Edit your content
<a id="link1" href="#">click to Edit</a>
<textarea id="text1" cols="3"/>
<input type="button" id="button1" value="Save" />
</div>
What i want is..
-
I want to show and hide the textarea and save button every time the link is clicked.
-
And when an save button clicked the textarea content has to be added before the textarea as listitem, every time the save button clicked the edited text has been updated into that list.
Please anyone guide me to do this..
The simples approach would be if the
Edit your contentpart could be wrapped in a container of its own, so that the content of that container could be replaced entirely upon save:Demo
You’ll note that the declaration of
parentcould easily be replaced with#div1in this particular example, but with this code, you could easily change the#div1 > aselector to one that matches several elements (i.e..editable > a; demo)Edit
It appears I misread your question the first time around, but the changes aren’t all that big.
Rather than setting the textbox to the value of your
.content, you would clear it each time you’re showing it. Also, you might not want to hide the.contenteach time the edit link is clicked. At the click of the save button, you create a new element, and append that after the last.content, rather than updating the existing one.Note that I’ve changed
.contentto adiv, because of its block-level behavior. This should of course be reflected in the initial markup as well.Demo
Edit (2)
To account for your question in comments, about adding the textarea and save button upon link click, you’d have to make a few changes. First of all, the link click listener would have to be updated with code to add the elements, and presumably with a first check to see whether or not they exist already (i.e. second click of link button):
Second, your listener
$('#div1 > input[type=button]')will no longer work exactly as written, because there is no such button in the document at the time when the selector is evaluated. To fix this, you could either use a live delegate, such as:Demo. (for earlier jQuery versions, use
.delegate(selector, event, handler)rather than.on(event, selector, handler).)… Or, you could add the listener immediately to the button as you’re creating it:
Demo
As a bonus, I’m adding focus to the textbox after showing it in these demos. You may want that as well.