Jump to content
Larry Ullman's Book Forums

Trying To Use Same Jqery Function More Than Once On A Page

Recommended Posts

This, I suppose, is as much an HTML question as a js or jQuery question so I'm not sure if this is the proper forum. But, here goes....


I'm trying to use a jQuery image rotater function in my "responsive" html design.


My Bootstrap 3 framework let's me display html layout differently depending on the screen size.


THE PROBLEM is that I want to use the same jQuery function (which uses the id #fader) to format the page differently according to the screen size. However, referencing the same #id more than once on a page is not allowed.


So, I end up making multiple versions of the function "fader" and just change the id name. That results in something that works but uses lots of code.


I'm looking for suggestions on how to handle this by using the function only once( I already tried changing "#fader" to ".fader" so to make it a class instead of an id but it did not work (I'm not a great js programmer however).


Please refer to this script sample below.

Here's the HTML code that accesses the function according to screen size. Here, I have simply made two copies of the function with a different id name for each (fader.txt and fader2.txt):

 <!--IMAGE FADER for DESKTOP screens-->
<div class="visible-lg">
  <script src="assets/js/fader.txt"></script>
    <div style="position:absolute;top:100px; left:650px">
  <div id="fader" class="visible-lg">
      <img src="images/standInImage.jpg" width="500">
      <img src="images/standin2.jpg" width="500">
      <img src="images/standin3.jpg" width="500">
      <img src="images/standin4.jpg" width="500">

<!--IMAGE FADER for TABLET screens-->
<div class="visible-md visible-sm">
  <script src="assets/js/fader2.txt"></script>
    <div style="position:absolute;top:150px; left:450px">
  <div id="fader2">
      <img src="images/standInImage.jpg" width="350">
      <img src="images/standin2.jpg" width="350">
      <img src="images/standin3.jpg" width="350">
      <img src="images/standin4.jpg" width="350">

Here's the jQuery code itself using "#fader"

    $(function() {
    $("#fader img:not(:first)").hide();
    $("#fader img").css("position", "absolute");
    $("#fader img").css("top", "0px");
    $("#fader img").css("left", "50%");
    $("#fader img").each(function() {
        var img = $(this);
        $("<img>").attr("src", $(this).attr("src")).load(function() {
            img.css("margin-left", -this.width / 2 + "px");

    var pause = false;
    function fadeNext() {
        $("#fader img").first().fadeOut().appendTo($("#fader"));
        $("#fader img").first().fadeIn();

    function fadePrev() {
        $("#fader img").first().fadeOut();
        $("#fader img").last().prependTo($("#fader")).fadeIn();

    $("#fader, #next").click(function() {

    $("#prev").click(function() {

/*    $("#fader, .button").hover(function() {
        pause = true;
    },function() {
        pause = false;

    function doRotate() {
        if(!pause) {
    var rotate = setInterval(doRotate, 5000);

I will need a total of 4 layouts for 4 different screen sizes for a single html page. I hope I can do this without having to load 4 copies of the same script but with different id names.


Thank you. I hope I have stated this problem clearly.

Link to post
Share on other sites
  • 2 weeks later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...