简体   繁体   中英

how can I put javascript variables in a html action=“”

I want to be able to put my variable in my html action="" so that i can change the search engine I use in one click. instead of having to make another html page.

<!DOCTYPE HTML>
<html>
<head>
<link rel="stylesheet" href="style.css">
  </head>
  <body>
    <div class="searchbar">
    <form action="[SEARCH VAR]" method="get">
      <input autocomplete="off" type="text" name="q" placeholder=searchvar id="mySearch">
      </form>
    </p>
    <button type="button" onclick="var search=https://www.duckduckgo.com/">
    duckduckgo
  </button>
  <p id="demo"></p>
      </div>
    </body>
    </html>

is there anyway to do it with this code.

You cannot have arbitrary HTML attributes be evaluated as JavaScript. Instead you'd use the DOM API to change the action property of the form element.

function setSearchEngine(engine) {
    document.querySelector('form').action = engine;
}

Use as

<button type="button" onclick="setSearchEngine('https://www.duckduckgo.com/')">
  duckduckgo
</button>

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