Skip to main content
Inspiring
November 30, 2017
Question

need help adding a javascript var to a form url

Hi all,

I need help adding a javascript var to a form url.

So the form url is like:

"url": "https://mypage/info/230",

but I want to replace the number 230 with var MyNum like:

var MyNum = 230;

"url": "https://localhost/mypage/info/" + MyNum + "\"",

but it doesn't seem to work

Is there a better way to write it?

var settings = {

  "async": true,

  "url": "https://localhost/mypage/info/230",

  "method": "Post",

  more stuff.....

}

This topic is closed to new replies. Start a new post to keep the conversation going.

5 replies

sinious
Legend
January 11, 2018

Watch out for typos like setting the object value how you are.

e.g.: "url": "https://localhost/mypage/info/" + MyNum + "\"",

That is setting the property .url to: https://localhost/mypage/info/MyNum"

Note the double quote at the end because you specified to end it with "\"". It doesn't take much to break things like that.

Translate
revdaveAuthor
Inspiring
December 2, 2017

Hi - I could still use some help with this - thanks

Translate
sinious
Legend
December 5, 2017

You can just change the .action property in JavaScript. Short example:

Example HTML:

<form id="myForm">

<input type="text" id="theInput" value="/script.php">

<button type="button" id="changeForm">Update Action</button>

</form>

Example JS:

(function(){

document.getElementById('changeForm').onclick = function(e) {

    document.getElementById('myForm').action = document.getElementById('theInput').value;

  }

})();

JSFiddle: JSfiddle

This is just assigning a click handler to the button that adjusts the .action property of the form "myForm" to whatever you put in the input text field.

Translate