bedipavel
bedipavel

Reputation: 13

Add variable to attribute

I need to attribute action url to be composed of "url" + variable. Like this:

    <script>
    var domain = "@example.com";
    </script>
    <body>
    <form action=("https://login.example.com/&" + domain) id="login_form" method="get">
    <input type="text" name="username">
    <button type="submit" id="submit">Log in</button>
    </form>

I need to make label with lastname and send the label as [email protected] by this form.

Upvotes: 0

Views: 94

Answers (4)

Aayush Mall
Aayush Mall

Reputation: 1013

You can set the form action dynamically with the help of Javascript after page load.

Example:

var frm = document.getElementById('login_form');
var domain = "@camosoft.cz";
var action = "https://login.szn.cz//?returnURL=https://email.seznam.cz/&serviceId=email&" + domain
if(frm) {
   frm.action = action;
}

Upvotes: 0

Scott Marcus
Scott Marcus

Reputation: 65808

You have to concatenate the string and then set the final value via JavaScript, not inline in HTML.

Also, place your script element just before the closing body tag (</body>) so that by the time the parser reaches it, all the HTML will have been parsed into memory.

Lastly, the parenthesis in your HTML around your attribute value is incorrect.

<body>
  <form action="https://login.szn.cz//?returnURL=https://email.seznam.cz/&serviceId=email&" id="login_form" method="get">
    
  </form>
  <script>
    var domain = "@camosoft.cz";

    // concatenate the domain on to the end of the current action:
    document.querySelector("form").action += domain;

    console.log(document.querySelector("form").action);  // <-- Verify results
  </script>
</body>

Upvotes: 0

g8bhawani
g8bhawani

Reputation: 674

You can try this

<script>
    var domain = "@camosoft.cz";
    var url = "https://login.szn.cz//?returnURL=https://email.seznam.cz/&serviceId=email&" + domain
    document.getElementsById("login_form").setAttribute("action", url);
</script>
<body>
<form action="" id="login_form" method="get">

Upvotes: 2

ellipsis
ellipsis

Reputation: 12152

Get the form element using document.querySelector. Using setAttribute add the variable to the action attribute and give it to the form

    var domain = "@camosoft.cz";
    var a=document.querySelector('form').getAttribute('action');
    document.querySelector('form').setAttribute('action',a+domain);
    <body>
    <form action="https://login.szn.cz//?returnURL=https://email.seznam.cz/&serviceId=email&" + domain id="login_form" method="get">d</form>

Upvotes: 0

Related Questions