on 07-23-2006 8:43 AM
Hi,
Is there a way to pass a value from JS to HTML ?
Here is my HTML form :
<html>
<form name="input" action="http://server:port/webdynpro/dispatcher/local/WD11111/MyApp" method="get">
<p>
<script type="text/javascript">
function getCurrentURL() {
var url = window.location.href
document.write(url)
return url
}
</script>
</p>
URL:
<input type="text" name="url" value="getCurrentURL">
<br>
<input type="submit" value="Submit" >
</form>
</html>
I want to pass to the URL field the result of the function getCurrentURL.
Thanks in advance.
Couple of ways of doing it.
document.getElementByID('url').value = url
You would just need to add an id="url" to your input tag.
The other way
input.url.value = url
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Sorry I should have included this line to demostrate
<input type="text" name="url" id="url" value="">
So if you look at the whole thing at once it would look like this:
<html>
<head>
<title>My Test Page</title>
<script type="text/javascript">
function getCurrentURL() {
var url = window.location.href
return url
}
</script>
</head>
<body>
<form name="input" action="http://server:port/webdynpro/dispatcher/local/WD11111/MyApp" method="get">
URL: <input type="text" name="url" id="url" value="">
<br>
<input type="submit" value="Submit" >
</form>
<script type="text/javascript">
document.getElementById('url').value = getCurrentURL();
</script>
</body>
</html>
the other option looks like:
<html>
<head>
<title>My Test Page</title>
<script type="text/javascript">
function getCurrentURL() {
var url = window.location.href
return url
}
</script>
</head>
<body>
<form name="input" action="http://server:port/webdynpro/dispatcher/local/WD11111/MyApp" method="get">
URL: <input type="text" name="url" id="url" value="">
<br>
<input type="submit" value="Submit" >
</form>
<script type="text/javascript">
// Take the form name then the element then the property
input.url.value = getCurrentURL();
</script>
</body>
</html>
another option could be:
<html>
<head>
<title>My Test Page</title>
</head>
<body>
<form name="input" action="http://server:port/webdynpro/dispatcher/local/WD11111/MyApp" method="get">
<script type="text/javascript">
document.write('URL: <input type="text" name="url" id="url" value="'+window.location.href+'">');
</script>
<br>
<input type="submit" value="Submit" >
</form>
</body>
</html>
Slight differences in each of the three but they all work.
Please check this expamle can help you more.
Here text box will be populated by the current URL when it will load in browser.
<html>
<head>
<script type="text/javascript">
function load() {
var url = window.location.href
document.input.url.value=url;
}
</script>
</head>
<body>
<body onload="load()">
</body>
<form name="input" action="http://server:port/webdynpro/dispatcher/local/WD11111/MyApp" method="get">
URL:
<input type="text" name="url" value="">
<br>
<input type="submit" value="Submit" >
</form>
</html>
<html>
<head>
<script type="text/javascript">
function load() {
var url = window.location.href
alert("URL:"+url);
document.input.url.value=url;
}
</script>
</head>
<body>
<body onload="load()">
</body>
<form name="input" action="http://server:port/webdynpro/dispatcher/local/WD11111/MyApp" method="get">
URL:
<input type="text" name="url" value="">
<br>
<input type="submit" value="Submit" >
</form>
</html>
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.