Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 627
  • Last Modified:

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
0
EazyWorks
Asked:
EazyWorks
  • 6
  • 4
  • 2
1 Solution
 
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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
 
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 6
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now