0

I'm trying to learn jquery. So, first I thought of validating a textbox and check if its length is less than 4 display a stop image adjacent to it.Otherwise, if it is greater than 4 display tick image. SO, i did this

   $(document).ready(function () {
      $('#' + '<%= tbstreet1.ClientID %>').keyup(isValid);
       function isValid() {
          var street1Length = $('#' + '<%= tbstreet1.ClientID %>').val().length;

          if (street1Length > 4) {
              ShowStop(check);
          }
      };
      function ShowStop(isCheck) {
          if ($('#' + '<%= tbstreet1.ClientID %>').blur()) {
              if (isCheck == true) {
                  $('#' + '<%= status.ClientID %>').html('<img align="absmiddle" 
                 src="~/tick.gif" /> ');
              }
              else
                  $('#' + '<%= status.ClientID %>').html('<img align="absmiddle" 
                  src="~/stop.gif" /> ');
          }
      };
      function ActivateSave() {
          $("#MainContent_btn_save").removeAttr("disabled");
          $("#MainContent_btn_save").attr("enabled", "");
      };
  }); 

This is my aspx code

   <asp:Label runat="server" ID="street1" Text="Street1" ></asp:Label> 
   <asp:TextBox ID="tbstreet1" runat="server"  ></asp:TextBox>  
   <div id="status" runat="server"></div> 

However, when i run it i dont get the image. Can u please let me know the mistake i've been doing?

1
  • I know this doesn't directly answer your question, but have you considered setting a breakpoint in isValid() and stepping through the code? One way to do it: in Chrome, right-click on the page and choose "Inspect Element." Then use the Scripts tab and select your JavaScript code. This is generally incredibly helpful as you get a hang of JQuery/JS. Commented Sep 16, 2011 at 5:52

4 Answers 4

1
function ShowStop(isCheck) {
    if ($('#' + '<%= tbstreet1.ClientID %>').blur()) { // <-- doesn't make any sense, might work if remove...
        if (isCheck == true) {
              $('#' + '<%= status.ClientID %>').html('<img align="absmiddle" 
             src="~/tick.gif" /> ');
        }
          else
              $('#' + '<%= status.ClientID %>').html('<img align="absmiddle" 
              src="~/stop.gif" /> ');
        }
};

should be,

function ShowStop(isCheck) {
        if (isCheck == true) {

              $('#' + '<%= status.ClientID %>').html('<img align="absmiddle" 
              src="~/tick.gif" /> ');

        } else {

              $('#' + '<%= status.ClientID %>').html('<img align="absmiddle" 
              src="~/stop.gif" /> ');
        }
};
Sign up to request clarification or add additional context in comments.

1 Comment

+1 for "doesn't make sense" but it should still be true and get you to the if (isCheck == true), jsfiddle.net/ambiguous/TrvPh
1

You haven't declared check anywhere so you will get a JavaScript error right here:

ShowStop(check);

1 Comment

+1 true! haha nice one... and also check my answer below... :D
0

Probably because it's not resolving the path to image, so either provide the complete path like this

<img align="absmiddle" src="images/stop.gif" />

or use Page.ResolveUrl to get an absolute path liek this

<img align="absmiddle" src='<%=ResolveUrl("~/stop.gif") %>' />

4 Comments

my image is directly in the project at root level
then it should be: <img align="absmiddle" src="stop.gif" />
tried that. not working, i dont think its the probelm with image. I think it is not going into the diplay image code
actually, if ($('#' + '<%= tbstreet1.ClientID %>').blur()) { line doesn't make any sense there... remove it and I think you're good to go...
0

~/ path notation does not work in Javascript:

Instead of

$('#' + '<%= status.ClientID %>').html('<img align="absmiddle" src="~/tick.gif" /> ');

Use

$('#' + '<%= status.ClientID %>').html('<img align="absmiddle" src="<%=ResolveUrl("~/tick.gif") %>" /> ');

2 Comments

tried that. not working, i dont think its the probelm with image. I think it is not going into the diplay image code
The server won't process ~/ unless it's within a server control.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.