cancel
Showing results for 
Search instead for 
Did you mean: 

Passing values from JavaScript to HTML

david_fryda2
Participant
0 Kudos

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.

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

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

david_fryda2
Participant
0 Kudos

Hi Craig,

Can you please tell me how to define a tag in HTML...I am quite novice at it.

Is it like :


<html id="url">

Is it the only change I must do ?

Thanks.

Message was edited by: David Fryda

Former Member
0 Kudos

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.

gregorw
Active Contributor
0 Kudos

Hello David,

so I suggest that you should have a look at:

http://en.selfhtml.org/

Regards

Gregor

david_fryda2
Participant
0 Kudos

Thanks a lot for your precious help.

Regards.

Former Member
0 Kudos

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>

Former Member
0 Kudos

<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>

Answers (0)