0

I want to edit following code.(This code is come from [http://stackoverflow.com/a/9280414/945688]

<!DOCTYPE html>
<html>
  <head>
    <title>image</title>        
  </head>
  <body>

    <img id="img" src="1.jpg" />

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <script type="text/javascript">
      var interval = null;
      function changeImage(){
        var nextNum = parseInt($('#img').attr('src').replace('.jpg',''), 10) + 1;
        // If you want it to repeat back to 1 after 5
        nextNum = nextNum > 5 ? 1 : nextNum;
        $('#img').attr('src', nextNum+'.jpg');
      }

      $('#img').hover(
        function(){
          // Call change Image every 50ms
          interval = setInterval(changeImage, 50);
          changeImage();
        },
        function(){
          // Stop the call
          interval && clearInterval(interval);
        }
      );
    </script>
  </body>
</html>​​​​​​​​​​​​

The above code is fine work.But I want to substitute in image path.

If

    <?php $image_folder_path='imagenewfolder';?>
<img id="img" src="images/<?php echo $image_fold_path; ?>/1.jpg>

,How to edit jquery code .

10
  • i dont get your question, can you please help a little more? Commented Feb 16, 2012 at 5:22
  • See this link.[link]stackoverflow.com/a/9280414/945688.I want to change jquery code variable value with php. Commented Feb 16, 2012 at 5:33
  • which value do you want to change? Commented Feb 16, 2012 at 5:46
  • I want to change image path.Original image path is <img src='1.jpg>.I want to change php variable. <img src='images/<?php echo $imagefolder; ?>/1.jpg.But I don't know how to edit in jquery. Commented Feb 16, 2012 at 5:50
  • oh i think i get it, you mean your current naming scheme is 1.jpg,2.jpg etc, and you want to replace it with somefoldername/1.jpg, somefoldername/2.jpg? and your current jquery code is not working with this, if so will foldername be constant? Commented Feb 16, 2012 at 5:54

1 Answer 1

1

try this code.

var interval = null;
var imgPath = '<?php echo $image_path; ?>'; // it should have a trailing slash
      function changeImage(){
        var nextNum = parseInt($('#img').attr('src').replace(imgPath,'').replace('.jpg',''), 10) + 1;
        // If you want it to repeat back to 1 after 5
        nextNum = nextNum > 5 ? 1 : nextNum;
        $('#img').attr('src', imgPath + nextNum+'.jpg');
      }

      $('#img').hover(
        function(){
          // Call change Image every 50ms
          interval = setInterval(changeImage, 50);
          changeImage();
        },
        function(){
          // Stop the call
          interval && clearInterval(interval);
        }
      );
Sign up to request clarification or add additional context in comments.

Comments

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.