need help with javascript

G

Guest

Hi,
I have been trying for an hour now with this. I need to pass the value from
one textbox1 to textbox2, when a button is clicked, using javascript.
textbox2 is within a panel.

This is the script

<script type="text/javascript">

function onOk()
{

var x = document.all.Textbox2.Value;
//var x = document.getElementById("Textbox2").Value;
//document.getElementById('<%= Textbox2.Text %>').innerText;

document.getElementById("Textbox1").value=x;
}
</script>

Please advice.

Thanks
 
D

David Hogue

Chris said:
Hi,
I have been trying for an hour now with this. I need to pass the value from
one textbox1 to textbox2, when a button is clicked, using javascript.
textbox2 is within a panel.

This is the script

<script type="text/javascript">

function onOk()
{

var x = document.all.Textbox2.Value;
//var x = document.getElementById("Textbox2").Value;
//document.getElementById('<%= Textbox2.Text %>').innerText;

document.getElementById("Textbox1").value=x;
}
</script>

Please advice.

Thanks

Hey Chris,

Try using "document.getElementById('<%= Textbox1.ClientID %>')" to get
the element. Another route would be to browse to the page, view the
rendered html, and get the id from there. ClientID will be more
flexible should your page change.

Below is my go at it. You didn't include the markup for the textboxes
or the panel, so I can't be sure this will work.

function onOk()
{
var textbox1 = document.getElementById('<%= Textbox1.ClientID %>');
var textbox2 = document.getElementById('<%= Textbox2.ClientID %>');
textbox1.value = textbox2.value;
}

Note that document.all is an Internet Explorer only feature and
document.getElementById is the recommended way. innerText is also ie
only. value should be lower case. Some browsers ignore case, but
others don't.
 
G

Guest

Works like a charm. Thanks! Wher can I find documentation on using these
Javascript methods and properties like document.getElementById and value?
Thanks
 
G

Guest

Also, how can I reset a value to clear the textbox?
Do I use

document.getElementById('<%= Textbox1.ClientID %>').value="";

Thanks
 
D

David Hogue

Chris said:
Works like a charm. Thanks! Wher can I find documentation on using these
Javascript methods and properties like document.getElementById and value?

Here's a few I use from time to time:

http://www.quirksmode.org/
http://developer.mozilla.org/en/docs/JavaScript
http://msdn.microsoft.com/workshop/author/dhtml/reference/methods/getelementbyid.asp
http://www.w3.org/DOM/
Also, how can I reset a value to clear the textbox?
Do I use

document.getElementById('<%= Textbox1.ClientID %>').value="";

That should work just fine.

Happy to help
 
L

Laurent Bugnion

Hi,
Also, how can I reset a value to clear the textbox?
Do I use

document.getElementById('<%= Textbox1.ClientID %>').value="";

Thanks

Exactly.

HTH,
Laurent
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top