Update textarea and display new contents
What I'm trying to do is have some text on a page. Click the text and it turns into a textarea. Then you change the text, and click the update link and it will update the text.
This first example will not work because in the update_title() function I'm trying to get the textarea contents with .text(). This will not work. You must use the .val() method.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Click text to convert to textarea</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<!--<link rel="stylesheet" href="css/default.css" type="text/css" />-->
<style type="text/css">
body {
font-family:sans-serif;
font-size:100%;
}
div.box {
margin:8px;
padding:8px;
border:1px solid gray;
background-color:silver;
width:200px;
text-align:center;
}
textarea {
width:100%;
margin:0;
padding:0;
}
.jslink {
cursor:pointer;
}
</style>
<script src="jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
});
function change_title(e)
{
var curtitle = $(e).text();
$(e).parent("div.box").html("<textarea>" + curtitle + "</textarea><br /><a class=\"jslink\" onclick=\"update_title(this)\">Update</a>");
}
function update_title(e)
{
var newtitle = $(e).siblings("textarea").text();
alert(newtitle);
}
</script>
</head>
<body>
<div id="container">
<div class="box">
<div class="title" onclick="change_title(this)">This is a title</div><!--div.title-->
</div><!--div.box-->
</div><!--container-->
</body>
</html>
This example is the exact same was above but with .val() used in the update_title() function instead of .text().
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Click text to convert to textarea</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<!--<link rel="stylesheet" href="css/default.css" type="text/css" />-->
<style type="text/css">
body {
font-family:sans-serif;
font-size:100%;
}
div.box {
margin:8px;
padding:8px;
border:1px solid gray;
background-color:silver;
width:200px;
text-align:center;
}
textarea {
width:100%;
margin:0;
padding:0;
}
.jslink {
cursor:pointer;
}
</style>
<script src="jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
});
function change_title(e)
{
var curtitle = $(e).text();
$(e).parent("div.box").html("<textarea>" + curtitle + "</textarea><br /><a class=\"jslink\" onclick=\"update_title(this)\">Update</a>");
}
function update_title(e)
{
var newtitle = $(e).siblings("textarea").val();
alert(newtitle);
}
</script>
</head>
<body>
<div id="container">
<div class="box">
<div class="title" onclick="change_title(this)">This is a title</div><!--div.title-->
</div><!--div.box-->
</div><!--container-->
</body>
</html>
The most important part of this process is the difference between the update_title() and change_title() functions.
In the change_title() function where we initially get the div's content, we use the .text() method. Whereas in the update_title() function we use the .val() method. The .val() method will not work in the change_title() function.
[Click to add or edit comments])
Please prepend comments below including a date