I'm trying to use jQuery to find the text within H1 then use this to replace the text already in foo2. Example below:
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Take text from element</title>
<script type="text/javascript">
var str = $("h1").text();
$("#foo2").html(str);
$("#foo2").replaceWith( str );
</script>
</head>
<body>
<div id="foo"><div id="foo2">replace with h1</div></div>
<h1>Text I want to take</h1>
</body>
</html>
<script type="text/javascript">
$(function() {
var str = $("h1").text();
$("#foo2").html(str);
$("#foo2").replaceWith( str );
});
</script>
Your code works, just put $(function() { // your code here }); around it.
$("#foo2").text($("h1").text());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With