Copy link to clipboard
Copied
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.....
}
Copy link to clipboard
Copied
Hi - I could still use some help with this - thanks
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Can you not set it as below:
<script>
var MyNum = 230;
var url = "https://localhost/mypage/info/"+MyNum;
var settings = {
"async": true,
"url": url,
"method": "Post"
};
Copy link to clipboard
Copied
Can you explain what exactly does not work? Maybe we can pick up from there.
Ia there an error message?
What is the behavior and what is the expected behavior?
Thabk you.
Copy link to clipboard
Copied
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.