0

I have the following code-

<script type="text/javascript">
    $(document).ready(function() {
        $("#clanname").keyup(function(e) {

            var clanname = $(this).val();
            if (clanname.length < 4) {
                $("#user-result").html('');
                return;
            }
        if (clanname.length >= 4) {
            $("#user-result").html('<img src="images/ajax-loader.gif" />');
            $.get('getClanName', {clanname:clanname}, function(data) {   

                    if(data=='free')
                    {
                    $("#user-result").html('<img src="images/available.png" />');

                }else{
                    $("#user-result").html('<img src="images/not-available.png" />');
                }
                });
            }
        });
    });
    </script>

which I put in the head section of my php file, but I want to put this in a seperate .js file and want to call this code on the text changed event of my textbox.and I want to make it parameterized function and want to pass the textbox's text to that function Can somene please suggest me how to to this.

1
  • Put your code in fuction and call it when text change occur. Commented Mar 21, 2014 at 6:19

2 Answers 2

1

1) Put following code in new doc:

$(document).ready(function () {

   //your function 

});

2) Save this file as myfile.js

3) In php file put this reference:

<script src="path_to_jsfile_if_not_in_the_same_dir_as_phpfile/myfile.js" type="text/javascript"></script>
Sign up to request clarification or add additional context in comments.

Comments

0

Try this

create file like custom.js and paste this code in that file and save

$(document).ready(function() {
        $("#clanname").keyup(function(e) {

            var clanname = $(this).val();
            if (clanname.length < 4) {
                $("#user-result").html('');
                return;
            }
        if (clanname.length >= 4) {
            $("#user-result").html('<img src="images/ajax-loader.gif" />');
            $.get('getClanName', {clanname:clanname}, function(data) {   

                    if(data=='free')
                    {
                    $("#user-result").html('<img src="images/available.png" />');

                }else{
                    $("#user-result").html('<img src="images/not-available.png" />');
                }
                });
            }
        });
    });

In your page below <head> & below Jquery library js file add

<script type="text/javascript" src="js/custom.js"></script>

1 Comment

Sorry I forget to mention one thing- I want to make it parameterized function and want to pass the textbox's text to that function

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.