
<div id="textReadOnly"></div>
<div id="textEdit">
<form>
<textarea id="textArea" rows="5"></textarea>
<div id="buttons">
<button id="btnSave" class="btn">Spara</button>
<button id="btnCancel" class="btn">Avbryt</button>
</div>
</form>
</div>
// The following function is executed
//after the page/DOM is fully loaded
$(document).ready(function () {
// Perform HTTP GET as soon as page is loaded to get text
$.ajax({
url: "/website/GetText.php",
cache: false
}).done(function(data) {
$('#textReadOnly').html(data);
});
// Define action for click on read-only div
$("#textReadOnly").click( function(e) {
// Read content of div and put in text area
$("#textEdit").val( $("#textReadOnly").html() );
// View text area and hide read only area
$("#textReadOnly").hide();
$("#textEdit").show();
});
// Define action for click on cancel button
$("#btnCancel").click( function(e) {
$("#textEdit").hide();
$("#textReadOnly").show();
$("#textArea").val("");
});
// Define action for click on cancel button
$("#btnSave").click( function(e) {
// Call server to store modified text
// and get the (new) text back again
var text = $("#textArea").val();
$.ajax({
url: "/website/UpdateText.php",
data: {"text": text},
cache: false
}).done(function(data) {
$('#textReadOnly').html(data);
});
$("#textEdit").hide();
$("#textReadOnly").show();
$("#textArea").val("");
});
});
<div id="textReadOnly"></div>
<div id="textEdit">
<form>
<textarea id="textArea" rows="5"></textarea>
<div id="buttons">
<button id="btnSave" class="btn">Spara</button>
<button id="btnCancel" class="btn">Avbryt</button>
</div>
</form>
</div>
// The following function is executed
//after the page/DOM is fully loaded
$(document).ready(function () {
// Perform HTTP GET as soon as page is loaded to get text
$.ajax({
url: "/website/GetText.php",
cache: false
}).done(function(data) {
$('#textReadOnly').html(data);
});
// Define action for click on read-only div
$("#textReadOnly").click( function(e) {
// Read content of div and put in text area
$("#textEdit").val( $("#textReadOnly").html() );
// View text area and hide read only area
$("#textReadOnly").hide();
$("#textEdit").show();
});
// Define action for click on cancel button
$("#btnCancel").click( function(e) {
$("#textEdit").hide();
$("#textReadOnly").show();
$("#textArea").val("");
});
// Define action for click on cancel button
$("#btnSave").click( function(e) {
// Call server to store modified text
// and get the (new) text back again
var text = $("#textArea").val();
$.ajax({
url: "/website/UpdateText.php",
data: {"text": text},
cache: false
}).done(function(data) {
$('#textReadOnly').html(data);
});
$("#textEdit").hide();
$("#textReadOnly").show();
$("#textArea").val("");
});
});
<!DOCTYPE html>
<head>
<meta charset="utf-8" />
<title>index</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script>
// The following function is executed after page is fully loaded
$(document).ready(function () {
// Get all editable paragraphs
$('.editable').each(function() {
$(this).click(function(e) {
setClickEvent($(this));
}); // End of .click
}); // End of .each
}); // End of .ready
function setClickEvent(thisitem) {
html = ""
// Restore any textareas to paragraph text
$('textarea').each(function() {
var content = $(this).val();
html = '<p class="editable">' + content + '</p>'
$(this).outerHTML(html);
});
// Remove buttons from DOM
$('.buttonrow').each(function() {
$(this).remove();
});
// Get text in clicked paragraph
var text = thisitem.html();
// Build HTML to put in <p> section
html = '<textarea rows="5" cols="50">'
html = html + text
html = html + '</textarea>'
html = html + '<div class="buttonrow"><button type="button" id="btnSave">Spara</button> <button type="button" id="btnCancel">Avbryt</button></div>'
thisitem.outerHTML(html);
}
// jQuery outerHTML plugin
jQuery.fn.outerHTML = function(s) {
return (s)
? this.before(s).remove()
: jQuery("<p>").append(this.eq(0).clone()).html();
}
</script>
</head>
Du måste vara medlem för att kunna kommentera