?
Solved

Clear textbox text onfocus .net 2.0 web form

Posted on 2007-07-20
7
Medium Priority
?
4,506 Views
Last Modified: 2012-05-05
I have a couple textboxes that have values initially i.e. 'type text here' and when the user clicks or navigates to the textbox I want the text to disappear.  The textboxes are asp.net textboxes and are contained in a masterpage as well as ascx files.  I tried to use this code:

<script type="JavaScript">
    function clearText(boxId)
    {
      txtBox = document.getElementById(boxId);
      txtBox.value = "";
    }
</script>

Codebehind:
serviceTB.Attributes.Add("onfocus", "clearText(serviceTB);")

But this is not working as I keep getting object expected errors.  Any way to accomplish this in .net 2.0 when using masterpage?
0
Comment
Question by:egdigital
  • 4
  • 3
7 Comments
 
LVL 7

Expert Comment

by:ASPSQLServerCOM
ID: 19535107
try to use "this"
Code behind -
serviceTB.Attributes.Add("onfocus", "clearText(this);")
Javascript -

<script type="JavaScript">
    function clearText(obj)
    {
        obj.value = "";
    }
</script>

Thanks,
Dipesh


0
 

Author Comment

by:egdigital
ID: 19535150
Dipesh thanks for the quick response - I tried this and am still getting the error: object expected  code: 0
0
 
LVL 7

Expert Comment

by:ASPSQLServerCOM
ID: 19535183
Hi,

First check the rendered HTML source in browser (if you can paste it for me then it can be userful)
Check what is the value coming in "this", check what is the parameter.
use alert[obj.ID] to get the text box name and varify you are getting any value in object.

Thanks,
Dipesh

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:egdigital
ID: 19535331
ok here is what i tried:

<script type="JavaScript">
    function clearText(obj)
    {
        obj.value = "";
        alert[obj.ID];
    }
</script>


Rendered HTML For:  serviceTB.Attributes.Add("onfocus", "clearText(ctl00_serviceTB);")
<input name="ctl00$serviceTB" type="text" value="Service e.g. Piano Lesson" id="ctl00_serviceTB" class="FontTextFieldsDefault" onfocus="clearText(ctl00_serviceTB);" style="width:141px;" />

Rendered HTML For:  serviceTB.Attributes.Add("onfocus", "clearText(this);")
<input name="ctl00$serviceTB" type="text" value="Service e.g. Piano Lesson" id="ctl00_serviceTB" class="FontTextFieldsDefault" onfocus="clearText(this);" style="width:141px;" />

In both cases the text does not clear and an alert box does not pop-up i get the same object error.

Thanks
0
 
LVL 7

Accepted Solution

by:
ASPSQLServerCOM earned 2000 total points
ID: 19535378
use

<script language="javascript">
<!--
    function clearText(obj)
    {        
        alert(obj.id);
        obj.value = "";
    }
//-->
</script>
0
 
LVL 7

Expert Comment

by:ASPSQLServerCOM
ID: 19535390
Hi,

for code behind use
serviceTB.Attributes.Add("onfocus", "javascript:clearText(this);

Thanks,
Dipesh
0
 

Author Comment

by:egdigital
ID: 19535448
weird by changing the type to language ie using:
<script language="javascript">
<!--
    function clearText(obj)
    {        
        alert(obj.id);
        obj.value = "";
    }
//-->
</script>

instead of :

<script type="JavaScript">
    function clearText(obj)
    {
        obj.value = "";
        alert[obj.ID];
    }
</script>

it worked with this in codebehind: serviceTB.Attributes.Add("onfocus", "clearText(this);")

thanks for the help Dipesh
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
While opting for any web-to-print solution, you need to discuss with your team and some of your end users and know their opinions about your decisions. In this article we list down some questions you need to ask yourself.
This video teaches users how to migrate an existing Wordpress website to a new domain.
Wufoo.com provides powerful tools for surveying targeted groups, and utilizing data from completed surveys to find trends, discover areas of demand or customer expectation, and make business decisions on products or services.
Suggested Courses
Course of the Month16 days, 11 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question