简体   繁体   中英

HTML id block replacement

I'm learning cshtml/js and I'm not quite sure how to replace blocks of text. Here is my code:

    <!DOCTYPE html>
    <html>
    <head>

       <p id = "Jonny">Jonny the giant </p>
       <p> hi there <b id ="mytext"> dood! </b></p>

    <script>
        function Replace() {
            y = document.getElementById("mytext");
            y.innerHTML = "new string";             @* what I want to do -->  document.getElementById("Jonny");*@
        }
    </script>

    <button
        type = "button" onclick = "Replace()" > 'MAGIC SWAP'
    </button>

    </body>
    </html>

I want to replace the chunk of text with another chunk of text that I have identified with and id name

Looks like this is what you're after. document.getElementById("mytext").innerHTML = document.getElementById("Jonny").innerHTML;

Here's a cleaned up version of your HTML .

<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>

  <script>
  function Replace() {
    document.getElementById("mytext").innerHTML = document.getElementById("Jonny").innerHTML;
  }
</script>
</head>
<body>
  <p id="Jonny">Jonny the giant</p>
  <p>Hi there <b id="mytext"> dood! </b></p>

  <button type="button" onclick="Replace()">MAGIC SWAP</button>
</body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM