5

I have a function which I need to appear within a jQuery $(document).ready(function() {} - I am au fait with javascript but not really worked with jQuery. How can I jQuerify this function?

function populateContext()
{
    contextTxtBox = document.getElementById('searchContext');
    pathArr = window.location.pathname.split( '/' );
    contextTxtBox.value = pathArr[1].toUpperCase(); 
};
2
  • 3
    jQuery is JavaScript, it just offers some functions to make frequently used tasks easier, but also slower. This task is trivial, and is best left alone with simple Javascript, as it will be faster, and will have less dependences. Commented Sep 8, 2010 at 15:26
  • @Alexander. True...but it's trivial in this case. Commented Sep 8, 2010 at 15:39

7 Answers 7

4

jQuerify? Make it a plugin!

(function($){

    $.fn.populateContext = function(){
        var pathArr = window.location.pathname.split( '/' );
        return this.val(pathArr[1].toUpperCase());
    };


}(jQuery));

and use it like this

$(document).ready(function(){
    // Same as window.onload
    $("#searchContext").populateContext();
});
Sign up to request clarification or add additional context in comments.

1 Comment

Shucks, I just used up my last vote elsewhere so I can't up this one. But props for turning it into a plugin! True spirit of jQuery :)
3

It's actually nearly identical since the only thing I find worth jQuerifying (nice word) is the DOM element.

function populateContext()
{
    var contextTxtBox = $('#searchContext');
    var pathArr = window.location.pathname.split('/');
    contentTxtBox.val(pathArr[1].toUppercase());
}

$(document).ready(function()
{
    populateContext();
});

3 Comments

That was a typo, I meant val().
cane be simplified to $(document).ready(populateContext) if this all you're doing
can be simplified further to $(populateContext); if this all you're doing
1

this way, if i understand you correctly

function populateContext()
{
    contextTxtBox = $('#searchContext');
    pathArr = window.location.pathname.split( '/' );
    contextTxtBox.val(pathArr[1].toUpperCase()); 
};

Comments

1
$(document).ready(function() {
//whatever code you want

});

function populateContext()  {
    pathArr = window.location.pathname.split( '/' );
    $("#searchContext").Val(pathArr[1].toUpperCase()); 
};

just a sidenote: jQuery IS javascript so you can mix and match :)

Comments

1

This should do the trick

function populateContext() {
    var aPath = $( location ).attr( 'href' ).split( "/" );
    $( '#searchContext' ).val( aPath[1].toUpperCase() )
}

1 Comment

I seem to be the only one who is jQuerifying the location :)
0
function populateContext(){
    contentTxtBox = $('#searchContext');
    pathArr = window.location.pathname.split( '/' );
    $(contextTxtBox).val(pathArr[1].toUpperCase());
}

Comments

0

I am not sure aout what you mean, but you could just do:

$(document).ready(function() {
    populateContext()
});

If you want to improve your function to make use of jQuery, you could do this way:

function populateContext() {
    var $contextTxtBox = $('#searchContext');
    pathArr = window.location.pathname.split( '/' );
    $contextTxtBox.val(pathArr[1].toUpperCase()); 
};

If you provide more information, about what exactly is your doubt, i may be able to explain better.

2 Comments

My actual instructions are to add: on DOM loaded function listener 'DomLoaded' ----------- function populateContext() { contextTxtBox = document.getElementById('searchContext'); pathArr = window.location.pathname.split( '/' ); contextTxtBox.value = pathArr[1].toUpperCase(); } // populate the search context DomLoaded.load(populateContext); How does Jquery handle listeners??
It depends on the kind of event you are interested, in your case, is the DOM Ready event, you can simply: $(document).ready(callback); and the jQuery will call the "callback" function when DOM is ready. But i think you should read: api.jquery.com/ready it will make things more clear :)

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.