Change field value on page load

Hi everyone

Was wondering if some could help me with this cuz I’m pulling my hair out (Javascript newbie).

I have a form (below) and I need to use Javascript to change the name of one of the hidden fields when the page loads. This updated value will then be added to the URL on form submit to tell our server that Javascript is enabled.

I have been digging through some Javascript books I have and just need someone to help me put it all together. This is what I have so far:


<head>
<script type="text/javascript">
function updateValue() {
	var findField = document.getElementByID('js');
	var findField.value = "on";
}

window.onload = updateValue;
</script>
</head>

<body>
<form action="http://www.xyz.com/filename.aspx?action=[define]&text=[text]&searchlang=[searchlang]&deflang=[deflang]&product=[product]&searchtype=[searchtype]&js=[js]" method="get">
<input type="hidden" name="action" value="define" />
<input type="hidden" name="searchlang" value="english" />
<input type="hidden" name="deflang" value="english" />
<input type="hidden" name="product" value="xxx" />
<input type="hidden" name="searchtype" value="stemming" />
<input type="hidden" name="js" value="off" id="js" />
<label for="text">Search</label>
<input type="text" size="20" id="text" name="text" />
<input type="submit" value="search" size="5" class="button" />
</form>

Okay, so as you can see, I’m trying to change the value of the input with the an ID of “js”

Any help would be greatly appreciated.

Thanks
Mike

You’re trying to change the value I assume.
You got it almost.
Try the window.addEvent function written by Scott Andrew to attach an onload event properly.

Hi

Thanks for your reply. I found the addEvent function in the forum and have tried to customise it, but it isn’t working. The script I have put together is below. Please can you have a look and let me know where I am going wrong.


window.addEvent=function(e,ev,f,c){if(e.addEventListener){e.addEventListener(ev,f,c);}else if(e.attachEvent){var r=e.attachEvent('on'+ev,f);return r;}else{e['on'+ev]=f;}};

function updateValue(){
	var findField = document.getElementByID('js');
	var findField.value = "on";
}

window.addEvent(window, "load", updateValue, false);

Thanks
Mike

You’ve got some syntax errors my friend :slight_smile:

window.addEvent=function(e,ev,f,c){if(e.addEventListener){e.addEventListener(ev,f,c);}else if(e.attachEvent){var r=e.attachEvent('on'+ev,f);return r;}else{e['on'+ev]=f;}};
 
function updateValue(){ 
    var findField = document.getElementById('js'); // JS is case-sensitive
    findField.value = "on"; // var not needed, because findField was already defined
}

window.addEvent(window, "load", updateValue, false);

Hey hexburner

Thanks so much for your help. Was so frustrating to almost have been there but not quite. Gonna spend some more time in my Javascript books.

Thanks again for your help.

Mike