0

People are recommending me that I should convert this code to jquery, but how and where, are there any tools or how do you do that and why XMLHttpRequest is not good with jQuery??

I have this code:

function showVal(str) {
  if (str == "") {
    document.getElementById("txtHint").innerHTML = "* Please type in School Name.";
    return;
  }

  if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp = new XMLHttpRequest();
  } else {// code for IE6, IE5
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  }

  xmlhttp.onreadystatechange = function () {
    if (xmlhttp.readyState == 4) {
      if (xmlhttp.status == 200) { // break this into 2 statements so you can handle HTTP errors
        document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
      } else {
        document.getElementById("txtHint").innerHTML = "AJAX Error (HTTP "+xmlhttp.status+")";
      }
    }
  }; // functions declared in this way should be followed by a semi colon, since the function declaration is actually a statement.

  // encodeURIComponent() does all the escaping work for you - it is roughly analogous to PHP's urlencode()

  // xmlhttp.open("GET","ajaxFuncs2.php?q="+encodeURIComponent(str),true);
  xmlhttp.open("GET","ajaxFuncs2.php?q="+encodeURIComponent(str),true);

  xmlhttp.send();
}
5
  • 2
    You should begin by read some documentation about jQuery. Begin with the official website docs.jquery.com/How_jQuery_Works Commented Jan 17, 2012 at 17:09
  • 1
    If it works, there may not be any reason to switch. In fact, if you are not using (and loading) jQuery now, then converting to jQuery just adds to the size of the page. Commented Jan 17, 2012 at 17:10
  • It doesn't work with my other jquery code :S Commented Jan 17, 2012 at 17:12
  • @Kees: Does the OP really deserve +8 rep because he got downvoted? Commented Jan 17, 2012 at 17:28
  • 1
    @JustinSatyr well... if nobody explains the down vote, then that is the consequence. You can't spank someone without explaining why IMO. (Rep is +3) Commented Jan 18, 2012 at 18:56

2 Answers 2

2

If you really wanted to, conversion would make your code much more compact...but with the added penalty of having a dependency on jQuery. Personally, if you're comfortable handling the cross browser issues for this relatively simple piece of functionality I see no reason to convert it.

That being said, it'd be relatively easy to convert:

function showVal(str){
    if(str == ''){
        $('#txtHint').html("* Please type in School Name.");
        return;
    }

    $.get({
        url: 'ajaxFuncs2.php?q='+encodeURIComponent(str),
        success: function(data){ $('#txtHint').html(data); },
        error: function(){ $('#txtHint').html('AJAX Error'); }
    });
}

...or thereabouts

Sign up to request clarification or add additional context in comments.

1 Comment

@Drazek - It should at least be enough to get you started in the right direction. It's impossible for me to write your code for you without knowing any requirements.
0

You could rewrite it using jQuery's AJAX engine. It should be an easy task.

But you have to ask yourself the age old question: should I fix what ain't broken?

Using $.ajax it would look like this:

function showVal(str){ 
    if(str == null || str == ''){ 
        $('#txtHint').html('* Please type in School Name.');
    }
    else {
        $.ajax({ 
                url: 'ajaxFuncs2.php?q='+encodeURIComponent(str), 
                success: function(data, textStatus, jqXHR){ $('#txtHint').html(data); },
                error: function(request, status, error){ $('#txtHint').html('AJAX Error (HTTP ' + status + ')'); } 
            }); 
    }
} 

3 Comments

Using jQuery could make life a little bit easier, because you see what the code does instead of all the browser specifics.
it doesn't work, and how it can be easy task, when I have no javascript knowledge
@Drazek well... you'll need some basic JavaScript knowledgde to program JavaScript. But there are many resources on that (like w3schools.com/jquery/default.asp and w3schools.com/js/default.asp). Just read up a bit.

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.