Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find text from h1 and use it to replace foo2 text

Tags:

jquery

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>
like image 366
Dan382 Avatar asked Dec 18 '12 12:12

Dan382


2 Answers

<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.

like image 194
Sarah West Avatar answered Oct 18 '22 04:10

Sarah West


$("#foo2").text($("h1").text());
like image 25
Mahbub Avatar answered Oct 18 '22 02:10

Mahbub