简体   繁体   中英

Display html form values inside hyperlink in same page after submit using javaScript

This is a working script i found online

<html>
<head>
    <title>HTML JavaScript output on same page</title>
    <script type="text/JavaScript">
        function showMessage(){
            var message = document.getElementById("message").value;
            display_message.innerHTML= message;
        }
    </script>
</head>
<body>
<form>
Enter message: <input type="text" id = "message">
<input type="button" onclick="showMessage()" value="submit" />
</form>
<p> Message is: <span id = "display_message"></span> </p>
</body>
</html>

how can I show the output inside a hyperlink? i tried this but getting an error

<a href="https://google.com/<span id="display_message"></span>">Test</a>

Not sure of the question, but here is how to put it in a link and show that it is in the link.

<body>
  <body>
  <form>
  Enter message: <input type="text" id = "message">
  <input type="button" onclick="showMessage()" value="submit" />
  </form>
  <p> Message is: <a id="display_message" href="https://google.com/"></span></a> </p>

  <script>
    function showMessage(){
      const href = 'https://google.com/';
      var message = document.getElementById("message").value;
      display_message.innerHTML = href + message;
      display_message.href = href + message
    }
  </script>

  <script src="script.js"></script>
</body>

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