I will try to explain my question with a simpliefied version of the code I will leave out the ajax stuff.
What I want to do is retrieve a form via ajax modify some values and append it. This should be pretty easy but I just do not get it to work.
My code is…
<html>
<head>
<title>Test Jquery</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var str = "<div><form><textarea></textarea></form></div>"
$('a#action').on('click',function(e){
e.preventDefault();
var result = str;
var test = $("textarea",result).html("Hello world");
console.log(test);
console.log(result);
$(".sections").append(result);
});
});
</script>
</head>
<body>
<div id="content">
<a href="#" id="action">go action</a>
<div class="sections">
</div>
</div>
</body>
</html>#
The variable str should simulate the downloaded form from the server…
on click I want to change the
value
of the textarea and insert "Hello World"…
What I would like to see is my str variable modified and appended to the sections div…
I can modify the textarea within a subset
$("textarea",result).html("Hello world");
But I do not know how to reflect it to the whole result…
Like I said this is a trivial abstraction so str can differ…
Try the following:
DEMO.
Note that
$("textarea",result).html("Hello world")does not work in your example becasueresultis just a string; it needs to be either a DOM element or a jQuery object: