Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8206193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T08:35:08+00:00 2026-06-07T08:35:08+00:00

Possible Duplicate: jQuery – is it bad to have multiple $(document).ready(function() {}); Can you

  • 0

Possible Duplicate:
jQuery – is it bad to have multiple $(document).ready(function() {});
Can you have multiple $(document).ready(function() sections?

Here is my one and only jQuery file for the project

// loading search page
$(function(){
  $("#search").click(function() {
    $("#feature").load("templates/search.html", function() {
        $("#search-input").focus();
        $("#search-input").css({
            "margin-left": "20%",
            "margin-top" : "2%"
        });
    });
 });
});

/* 
finding youtube videos
API source : https://developers.google.com/youtube/2.0/developers_guide_jsonc
*/
$(function(){
    $('#search-input').live('keyup',function() {
        var search_input = $(this).val();
        var keyword = encodeURIComponent(search_input);
        var yt_url = 'http://gdata.youtube.com/feeds/api/videos?q='+keyword+'&format=5&max-results=10&v=2&alt=jsonc';

        $.ajax({
          url: yt_url,
          type: 'GET',
          dataType: 'jsonp',
          complete: function(xhr, textStatus) {
            //called when complete
          },
          success: function(response, textStatus, xhr) {
            if(response.data.items) {
                var template = $('#item').clone();
                $('#result').html(template);
                $.each(response.data.items, function(i, data) {
                    //console.log(data)
                    search_data = {
                        'id': data.id,
                        'title': data.title,
                        'views': data.viewCount,
                        'thumbnail': data.thumbnail['sqDefault'],
                    }
                    item = video_result_template(search_data);
                    $('#result').append(item).fadeIn('slow');
                });
            } else {

            }
          },
          error: function(xhr, textStatus, errorThrown) {
            //called when there is an error
          }
        });
    });
});

// filling out the search template
function video_result_template(data) {
    var item = $('#item').clone();
    item.removeClass('hide-item');
    item.find('img').attr('src', data.thumbnail);
    item.find('.title').html(data.title);
    item.find('.views').html(data.views);
    item.attr('id', data.id);
    item.addClass('view-item');
    return item;
}

// playing the video from search result on player pane
$(function(){
    $('.item').live('click', function(){
        // alert(this.id);
        var url = $('#video-frame').attr('src');
        var new_url = url.replace(/embed\/[\w -]*/g, 'embed/' + this.id);
        $('#video-frame').attr('src', new_url);
    });
});

// creating new playlist
$(function() {
    $('form input[type="text"]').live('keyup', function() {
        var val = $.trim(this.value);
        $('form .create-playlist-button').prop('disabled', val.length == 0).click(function(e){
            var title = $(e.target).closest('.video-detail').find('.title').text();
            alert(title);
        });
    });

    // $('form .create-playlist-button').live('click', function(e){
    //  var title = $(e.target).closest('.video-detail').find('.title').text();
    //  alert('title');
    // });
        // $('form .create-playlist-button').prop('disabled', val.length == 0).click(function(){
        //          alert($('.title').get(1).textContent);
        //      });

});

// animating slideshow on landing page
$(function(){
    $('#slides').slides({
        preload: true,
        pagination: true,
        preloadImage: '../static/img/loading.gif',
        play: 2000,
        pause: 1000,
        hoverPause: true
    });

    $('.slides_control').css({
        "height": "600px",
        "margin-right": "400px"
    });

    $('.pagination').hide('');
});

Question

  • This jquery page has multiple $(function)(means when DOM is ready) functions, is that incorrect? Given that
  • some jQuery functions apply to different pages
  • some element appear later on the page (see load() function)

How does all that work? I am not very sure, but it seems like there is something wrong here which means there is absolutely a chance to learn from people who do it regularily

P.S – I am absolutely new to the land of jQuery. I have been trying to work on my first project.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T08:35:09+00:00Added an answer on June 7, 2026 at 8:35 am

    YES you can use it in this way without any error, but to make your code more neat and readable you should not repeat the tags un-necessarily.

    Note: Variable declared in one function will not be accessible in other functions so you will need to be careful about variable scope in repeating function bodies.

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: jQuery - is it bad to have multiple $(document).ready(function() {}); Can you
Possible Duplicate: Bind multiple events to jQuery 'live' method I have the following function:
Possible Duplicate: Removing multiple classes (jQuery) how can we remove multiple classes from one
Possible Duplicate: JQuery - multiple $(document).ready … ? What are the implications of having
Possible Duplicate: jquery data selector I have several elements with class 'connection_name'. Each one
Possible Duplicate: jquery trigger - ‘change’ function I have a radio button set called
Possible Duplicate: jQuery: Return data after ajax call success I have the following function
Possible Duplicate: jQuery delay between animations I have created two functions: One that animates
Possible Duplicate: jquery - Is $(document).ready necessary? Putting the JS just above the </body>
Possible Duplicate: jQuery: how do I animate a div rotation? I have a div

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.