Manjiri Parab Manjiri Parab - 16 days ago 5
HTML Question

put variable value in textbox without onclick button in javascrip

I have made code with comments below:

which is storing value from url=> ../webdev/dummy.html?foo=quora, now my query is I want to show quora in textbox at the page load only, I don't want to use onclick function.please guide me :)

dummy.html

<script type="text/javascript">
function getParameterByName(name, url) {
if (!url) {
url = window.location.href;
}
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}

var foo = getParameterByName('foo');

document.getElementsByName('subject').Value = foo; // this is I am thinking of to store value in textbox id="subject"
</script>

<input type="text" name="subject" id="subject" readonly="true" />
<!-- textbox to display "quora" -->


it is not displaying any value in textbox

Answer

You have to replace the code

document.getElementsByName('subject').Value = foo;

with

document.getElementById('subject').value = foo;

In case you want to call it onload of the page you can use the following code,

body.onload=function(){//call your function};

or

window.onload=function(){//call the function}