Jquery v javascript

I have a function that should appear in jQuery $(document).ready(function() {}- I am au fait with javascript, but actually did not work with jQuery. How can I execute jQuerify this function?

function populateContext()
{
    contextTxtBox = document.getElementById('searchContext');
    pathArr = window.location.pathname.split( '/' );
    contextTxtBox.value = pathArr[1].toUpperCase(); 
};
+5
source share
7 answers

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();
});
+4
source

This is actually almost identical, since the only thing I find for 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
source

,

function populateContext()
{
    contextTxtBox = $('#searchContext');
    pathArr = window.location.pathname.split( '/' );
    contextTxtBox.val(pathArr[1].toUpperCase()); 
};
+1
$(document).ready(function() {
//whatever code you want

});

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

sidenote: jQuery IS javascript, :)

+1

function populateContext() {
    var aPath = $( location ).attr( 'href' ).split( "/" );
    $( '#searchContext' ).val( aPath[1].toUpperCase() )
}
+1
function populateContext(){
    contentTxtBox = $('#searchContext');
    pathArr = window.location.pathname.split( '/' );
    $(contextTxtBox).val(pathArr[1].toUpperCase());
}
0

, , :

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

, jQuery, :

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

If you provide more detailed information about what exactly you doubt, I can better explain.

0
source

All Articles