2 Replies - 1287 Views - Last Post: 10 June 2012 - 06:00 AM Rate Topic: -----

#1 R3cuRsioN   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 19-April 10

what's wrong with this jQuery code?

Posted 06 June 2012 - 05:13 PM

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">

<asp:Content ID="Content2" ContentPlaceHolderID="cpMainContent" Runat="Server">
    <div class="StateOneBtm">
        <div class="h2">blah blah blah</div>
        <div class="buttons">
            <asp:TextBox ID="Email" runat="server" style="margin-left: 0px" 
                Width="272px" Height="28px" ForeColor="#CCCCCC" 
                onfocus="Onfoc();" ClientIDMode="Static">Enter your email...</asp:TextBox>
            <asp:Button ID="Button1" runat="server" Text="Go" 
                CssClass="Button" Height="41px" onclick="Button1_Click" />
            <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" 
                ControlToValidate="Email" ErrorMessage="!" ForeColor="Red" 

    <script type="text/javascript" language="javascript">
        function Onfoc() {
            //document.getElementById('<%= Email.ClientID %>').value = "";
            //document.getElementById('<%= Email.ClientID %>').style.color = "#000";
            $('#Email').value = "";

Note that the textbox control with Id 'Email' has its onfocus event bound to the onFoc() function defined in the script.

Why won't this work? If I use the two lines that are commented out in the script it works just fine. When I try doing the same thing using jQuery it doesn't work. The EmailFocus css class is defined, and jQuery is included and everything.

Is This A Good Question/Topic? 0
  • +

Replies To: what's wrong with this jQuery code?

#2 mrburnttoast   User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 20
  • Joined: 28-December 11

Re: what's wrong with this jQuery code?

Posted 09 June 2012 - 12:58 AM

I think you just have incorrect syntax. This works:

$('#Email').each(function () {
            var email = this.value;
            $(this).focus(function () {
                if (this.value == email) {
                    this.value = '';
                    $(this).css('color', '#000');

I don't think you need the onfoc() method either.

MOD EDIT: Added code tags. When posting code...USE CODE TAGS!!!

This post has been edited by JackOfAllTrades: 09 June 2012 - 03:41 AM

Was This Post Helpful? 0
  • +
  • -

#3 Nakor   User is offline

  • Professional Lurker
  • member icon

Reputation: 448
  • View blog
  • Posts: 1,504
  • Joined: 28-April 09

Re: what's wrong with this jQuery code?

Posted 10 June 2012 - 06:00 AM

as mrburnttoast said, using $(this) should work. The reason just using $('#Email') doesn't work is because in asp.net the names of server controls get built by adding the name of their naming container, so the actual ID of the input element might end up being something like <input id='panel01_Email'... instead of just <input id='Email'.... The way you would find that element would be to use $('input[id$="Email"]') which will search for an element with an id ending in 'Email'. Of course if more than one element's id ends with 'Email' it will only select the first one it finds.

This post has been edited by Nakor: 10 June 2012 - 06:01 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1