Length doesn't work for ajax loaded content


Rayhan Porohit

Here is my HTML

 <li><a class="tab-click" href="#tab-4" data-tab-current="about-us">About Us</a></li>

Here is my jQuery code

  $(document).on('click', '.tab-click a', function(event) {
    if($('.s-tab').length)
        {
            event.preventDefault();
            $("html, body").animate({
                scrollTop: $('.tab').offset().top - 80
            }, 500);
            $('.main-tab > li').removeClass("current");
            $('[data-tab="'+$(this).attr("data-tab-current")+'"]').addClass("current");
            var tab = $(this).attr("href");
            $('.s-tab > div').not(tab).css("display", "none");
            $(tab).fadeIn();
        }
    else{

     alert('else');
    }

})

My html page content is loaded using ajax. When I try to find the "s-tab" class in my html document, I can't find it, when I check what is not loaded with ajax it works fine. Any help is greatly appreciated, thanks in advance.

Suresh suthar

Your click selector is wrong. try below code

 $(document).on('click', '.tab-click', function(event) {
if($('.s-tab').length)
    {
        event.preventDefault();
        $("html, body").animate({
            scrollTop: $('.tab').offset().top - 80
        }, 500);
        $('.main-tab > li').removeClass("current");
        $('[data-tab="'+$(this).attr("data-tab-current")+'"]').addClass("current");
        var tab = $(this).attr("href");
        $('.s-tab > div').not(tab).css("display", "none");
        $(tab).fadeIn();
    }
else{

 alert('else');
}

})

Related


jQuery focus doesn't work in Ajax loaded content

hairy I am trying to focus on an element generated using the ajax .loadmethod . DesignHome.aspx <!-- All Jquery Scripts Go Here --> <script src="../../Scripts/angular.min.js?v=7" type="text/javascript"></script> <script type="text/javascript"> var app = an

Triggers don't work for content loaded via Ajax

Walter White I have the following question. I was forced to rewrite all the following functions to create this function: $(document).on("change","#type_of",function(){ Instead of this: $('#type_of').on('change', function () { I'm doing this because I'm working

Bootstrap toggle doesn't work after ajax loaded

micif I am using ajax load to get some content on a page. I am using bootstrap 3 and bootstrap switch. The bootstrap 3 content works fine when loading the content (you can clearly see the panel panel-primary). But the bootloader content doesn't get loaded (you

Triggers don't work for content loaded via Ajax

Walter White I have the following question. I was forced to rewrite all the following functions to create this function: $(document).on("change","#type_of",function(){ Instead of this: $('#type_of').on('change', function () { I'm doing this because I'm working

AJAX submit won't work if contact form is in AJAX loaded content

Marco Marsala If the AJAX loaded content contains a CF7 contact form, the AJAX submission will not work for that form. The suggested workaround is to put this line in the Contact Form template: <script src="/wp-content/plugins/contact-form-7/includes/js/script

jQuery focus doesn't work in Ajax loaded content

hairy I am trying to focus on an element generated using the ajax .loadmethod . DesignHome.aspx <!-- All Jquery Scripts Go Here --> <script src="../../Scripts/angular.min.js?v=7" type="text/javascript"></script> <script type="text/javascript"> var app = an

jQuery doesn't trigger content loaded via Ajax

Tintyethan This works fine, but if the content is loaded via ajax it doesn't... $(function () { $('#defid_527209').on('change', function () { $('#formdef_527209').submit(); }); }); Clicking does nothing. The event doesn't f

jQuery autocomplete plugin doesn't work in Ajax loaded content

Gautamja jQuery demo page I'm having trouble getting auto-suggestions of countries for AJAX loaded content. in those ajax loaded forms. I searched everywhere for a solution to this problem, but I got some info about an autosuggest form server. I don't have a S

Form in Ajax loaded content doesn't work

who dr I have a page that loads content from other files (content.php)into a div that reloads every 5 seconds. In the content.php file I am using a form (simple HTML without javascript). When I access the file directly, (example.com/content.php)the form works,

jQuery focus doesn't work in Ajax loaded content

hairy I am trying to focus on an element generated using the ajax .loadmethod . DesignHome.aspx <!-- All Jquery Scripts Go Here --> <script src="../../Scripts/angular.min.js?v=7" type="text/javascript"></script> <script type="text/javascript"> var app = an

Triggers don't work for content loaded via Ajax

Walter White I have the following question. I was forced to rewrite all the following functions to create this function: $(document).on("change","#type_of",function(){ Instead of this: $('#type_of').on('change', function () { I'm doing this because I'm working

jQuery doesn't work with content returned by ajax

克里斯塔 普斯 · 盖杜里斯 (Kristaps Gaidulis) I've had this problem for a while now and I just can't understand what I'm doing wrong (I think it has something to do with the ajax response) I am trying to upload an image to the server. I've tried my luck with the uploadif

.click() doesn't work with ajax loaded content

good guy I have a datatable that fetches data from my server using AJAX. This code adds data to the datatable and also adds some buttons to it var jsdata = JSON.parse(data); for (var i = 0; i < jsdata.length; i++) {

Unobtrusive JavaScript doesn't work with AJAX loaded content

null I am facing very strange thing AJAXand Unobtrusive JavaScriptI have two pages ajaxcontent.php index.php index.php has <div id="cont"></div> <input type="button" id="a" value="load plz."> <script type="text/javascript"> document.getElementById('a').on

jQuery "open" doesn't trigger ajax loaded content

Android development When I retrieve the html using $.get(), the html contains form fields like input, textarea etc. I want the change, paste and keypress events to be attached to these controls, but these events are not fired. The jQuery documentation states t

Triggers don't work for content loaded via Ajax

Walter White I have the following question. I was forced to rewrite all the following functions to create this function: $(document).on("change","#type_of",function(){ Instead of this: $('#type_of').on('change', function () { I'm doing this because I'm working

jQuery focus doesn't work in Ajax loaded content

hairy I am trying to focus on an element generated using the ajax .loadmethod . DesignHome.aspx <!-- All Jquery Scripts Go Here --> <script src="../../Scripts/angular.min.js?v=7" type="text/javascript"></script> <script type="text/javascript"> var app = an

jQuery doesn't trigger content loaded via Ajax

Tintyethan This works fine, but if the content is loaded via ajax it doesn't... $(function () { $('#defid_527209').on('change', function () { $('#formdef_527209').submit(); }); }); Clicking does nothing. The event doesn't f

Triggers don't work for content loaded via Ajax

Walter White I have the following question. I was forced to rewrite all the following functions to create this function: $(document).on("change","#type_of",function(){ Instead of this: $('#type_of').on('change', function () { I'm doing this because I'm working

jQuery focus doesn't work in Ajax loaded content

hairy I am trying to focus on an element generated using the ajax .loadmethod . DesignHome.aspx <!-- All Jquery Scripts Go Here --> <script src="../../Scripts/angular.min.js?v=7" type="text/javascript"></script> <script type="text/javascript"> var app = an

Unobtrusive JavaScript doesn't work with AJAX loaded content

null I am facing very strange thing AJAXand Unobtrusive JavaScriptI have two pages ajaxcontent.php index.php index.php has <div id="cont"></div> <input type="button" id="a" value="load plz."> <script type="text/javascript"> document.getElementById('a').on

jQuery "open" doesn't trigger ajax loaded content

Android development When I retrieve the html using $.get(), the html contains form fields like input, textarea etc. I want the change, paste and keypress events to be attached to these controls, but these events are not fired. The jQuery documentation states t

jQuery doesn't work with content returned by ajax

克里斯塔 普斯 · 盖杜里斯 (Kristaps Gaidulis) I've had this problem for a while now and I just can't understand what I'm doing wrong (I think it has something to do with the ajax response) I am trying to upload an image to the server. I've tried my luck with the uploadif