0

I'm playing around with making a REST api and I'm working on some javascript functions.
The idea here is to run for example: $('#main').get('car/ford'); and the data returned will be added in the element provided.

Here is all the javascript:

$.fn.extend({
    get: function (path) {
        request(this, 'GET', path);
    }
});

function request(element, type, path) {
var dees = $(element);
    $.ajax({
        type: type,
        url: '/request/'+path,
        success: function(data) {
            console.log('Success');

            a = $(element);
            b = $('#fileList'); // this is a control

            dees.html(data);
        }
    });
}

(function() {
    console.log('running');
    $('#fileList').get('car/ford');
})();

The problem I'm having is that when I run a.html(data);
Nothing will change. But if i run b.html(data); Everything works like it should.

So there is a difference between those two selectors.
On a the element is not found a.length == 0
and on b the element is found b.length == 1

Why isn't the element found by the selector and how can I fix it?

3 Answers 3

2

The problem was solved by adding $ in front of the calling function.

From:

(function() {
    console.log('running');
    $('#fileList').get('car/ford');
})();

To:

$(function() {
    console.log('running');
    $('#fileList').get('car/ford');
});
Sign up to request clarification or add additional context in comments.

Comments

1

You could try the following:

function request(element, type, path) {
   var dees = $(element);
   $.ajax({
           type: type,
           url: '/request/'+path,
           success: function(data) {
               console.log('Success');
               dees.html(data);
           }
   });
}

in case the $(this) variable is conflicting with own $(this) variable of ajax() block.

1 Comment

Doesn't seem to be working. dees.length returns 0. But both you and Adil have the same answer so maybe there is another error.
1

Change element to $(element)

When request is call request(this, 'GET', path); this represents javascript object and it should be jQuery object. You need to pass jquery object or convert it to jquery object after being pass as I did.

$.fn.extend({
    get: function (path) {
        alert(this.tagName);
        var objToPass = $(this);  
        request(objToPass, 'GET', path);
    }
});
function request(javascriptObj, type, path) {  
    element = $(javascriptObj);   
    $.ajax({
        type: type,
        url: '/request/'+path,
        success: function(data) {
            console.log('Success');

            a = $(element);
            b = $('#fileList'); // this is a control

            a.html(data);
        }
    });
}

Update

The call to get function should be instantiated on document.ready which could be done by simply adding $

Change

(function() {
    console.log('running');
    $('#fileList').get('car/ford');
})();

To

$(function() {
    console.log('running');
    $('#fileList').get('car/ford');
})();

4 Comments

Doesn't seem to be working. element.length still returns 0. But both you and Nelson have the same answer so maybe there is another error.
There might be problem in calling function then, I have updated my answer and changed the caller function as well. Could you please give it a try.
Found the error (function() { doesnt work but $(function() { does
Good, You did a great job. Now converting javascript object to jquery will do its job.

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.