Building a On-Click JavaScript to fill in a text box.

I want to add a onclick event to a object, lets say a checkbox.
When the check box is clicked I want to fill in a text box with a value.
I currently have this on the page Load.

        Me.CheckBox1.Attributes.Add("onClick", "document.getElementById('TextBox2').value = 'New Value!';")
I am using a MasterPage (I think this is the issue) but I get an error saying it cannot find the object.

I also would like to know the syntax to but a string variable in the attributes also. Like
        Me.CheckBox1.Attributes.Add("onClick", "document.getElementById('TextBox2').value = '" & stringvariable  & "';")

Thanks
EazyWorksAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RoonaanCommented:
Can you show the html for your textbox2?

Make sure that you have id="TextBox2" as well as name="TextBox2" (although the name can be different)

-r-
0
EazyWorksAuthor Commented:
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
but it doesnt like name
0
RoonaanCommented:
Can you show the html when you do view source from your browser please.
Javascript in this case runs clientside so possibly its better to solve the clientside issue and then look what has to be done serverside to get the right result.

-r-
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

smaccariCommented:
You should use OnClientClick attribute, not onclick (onclick will reference a server side code)

Me.CheckBox1.Attributes.Add("onClientClick", "document.getElementById('TextBox2').value = 'New Value!';")
0
EazyWorksAuthor Commented:
<input name="ctl00$ContentPlaceHolder1$TextBox2" type="text" id="ctl00_ContentPlaceHolder1_TextBox2" /></div>
0
smaccariCommented:
mm ok maybe you should place a client id too in your control:

<asp:TextBox ID="TextBox2" ClientID="TextBox2" runat="server"></asp:TextBox>
0
EazyWorksAuthor Commented:
Comment from smaccari,
I don't think it can find the control right now and the "onClientClick" will not affect it but I will give it a try.
nope now it doesnt do anything, not even an error.
I think onClientClick is an ASP thing not a javascript thing
0
smaccariCommented:
It's an ASP.net attribute, as your text box is an ASP.net web control, and your code seems to be either JS.net, C# or VB.net - that is all server side.

What you try to do is to generate an onclick JS client side event handler.
Here, as your control is generated with an id of "ctl00_ContentPlaceHolder1_TextBox2", the client side script "document.getElementById('TextBox2')" will return undefined.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
EazyWorksAuthor Commented:
Comment from smaccari,
Ok Got that
Now how to use a variable for the  value in the
Me.CheckBox1.Attributes.Add("onClick", "document.getElementById('TextBox2').value = 'New Value!';")
to
Me.CheckBox1.Attributes.Add("onClick", "document.getElementById('TextBox2').value = '" & stringvariable  & "';")
0
EazyWorksAuthor Commented:
It is VB.Net
0
EazyWorksAuthor Commented:
Well I got it Thanks
        Me.CheckBox1.Attributes.Add("onClick", "document.getElementById('ctl00_ContentPlaceHolder1_TextBox2').value = '" & strText & "';")
0
smaccariCommented:
I think your syntax should work, if your variable stringvariable  is defined in your VB.net code of course.
If stringvariable is client side, then use:

Me.CheckBox1.Attributes.Add("onClick", "document.getElementById('TextBox2').value = stringvariable;")
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.