PDA

View Full Version : how to set value for hidden field



janunme
04-16-2007, 07:53 AM
hi to all

in my form there exist an hidden field. how to set a value to that field using javascript

regards

pcbrainbuster
04-16-2007, 08:01 AM
Well In JavaScript -

<html>
<body>
<input type="hidden" id="hiddenbox">
<script>
document.onload=function() {
document.getElementById('hiddenbox').value="The Value Here"
}
</script>
</body>
</html>

In Plain HTML -

<html>
<body>
<input type="hidden" value="The Value Here" id="hiddenbox">
</body>
</html>

Hope This Helps...

janunme
04-16-2007, 08:32 AM
thanks for your reply

i want to set the value hidden field when ever i click on a hyper link and also i want to set that hidden field value with this hyperlink value. is it possible.

regards

pcbrainbuster
04-16-2007, 08:33 AM
By value do you mean the href(address) or the text <a href="sadsasdsas.htm">WORDS</a>?

janunme
04-16-2007, 08:50 AM
i want send text value

pcbrainbuster
04-16-2007, 09:04 AM
OK... -

<html>
<body onclick="setavalue()">
<a href="#">OK</a>
<a href="#">What</a>
<input type="hidden" id="hiddenbox">
<script>
function setavalue() {
if (event.srcElement.tagName=="A") {
document.getElementById('hiddenbox').value=event.srcElement.innerText}
}
</script>
</body>
</html>

Though this may not work in other browsers then IE as I have not used e ...

janunme
04-16-2007, 09:23 AM
thanks for your reply


in my jsp page there exist hyper links from "A" to "Z" and when ever i click on any one of the letter once again it calls itself with a parameter as that clicked character.while loading i get the value which is passed through hidden field and executes a sql command and the result is plcaed below those hyper links.


regards

Twey
04-16-2007, 05:56 PM
<html>
<body onclick="setavalue()">
<a href="#">OK</a>
<a href="#">What</a>
<input type="hidden" id="hiddenbox">
<script>
function setavalue() {
if (event.srcElement.tagName=="A") {
document.getElementById('hiddenbox').value=event.srcElement.innerText}
}
</script>
</body>
</html>Inefficient (you only need to handle clicks on <a> elements, not any element), and yes, IE-only. You'd want to do something like this:
<script type="text/javascript">
onload = function() {
var f = function() {
document.getElementById("hiddenbox").value = this.text;
};
for(var i = 0, a = document.links; i < a.length; ++i)
a[i].onclick = f;
};
</script>

mburt
04-16-2007, 09:04 PM
Input fields don't have an "innerText" property anyways. They're tags which don't need to be closed.


document.getElementById("hiddenbox").value = this.text;
Calling that from an anchor... does an anchor even have a "text" property?

Twey
04-16-2007, 09:06 PM
Calling that from an anchor... does an anchor even have a "text" property?Indeed.