Chao  LI Chao LI - 6 months ago 13
Javascript Question

how to call jquery from php

var currentIndex = 0,
items = $('.container div'),
itemAmt = items.length;

function cycleItems() {
var item = $('.container div').eq(currentIndex);
items.hide();
item.css('display','inline-block');
}

var autoSlide = setInterval(function() {
currentIndex += 1;
if (currentIndex > itemAmt - 1) {
currentIndex = 0;
}
cycleItems();
}, 3000);

$('.next').click(function() {
clearInterval(autoSlide);
currentIndex += 1;
if (currentIndex > itemAmt - 1) {
currentIndex = 0;
}
cycleItems();
});

$('.prev').click(function() {
clearInterval(autoSlide);
currentIndex -= 1;
if (currentIndex < 0) {
currentIndex = itemAmt - 1;
}
cycleItems();
});


This is my jquery in my other file. Do you know how to call this script from my php page?


<button class="next">Next</button>
<button class="prev">Previous</button>
<div class="container">
<div style="display: inline-block;">
<img src="http://placeimg.com/400/200/people"/>
</div>
<div>
<img src="http://placeimg.com/400/200/any"/>
</div>
<div>
<img src="http://placeimg.com/400/200/nature"/>
</div>
<div>
<img src="http://placeimg.com/400/200/architecture"/>
</div>
<div>
<img src="http://placeimg.com/400/200/animals"/>
</div>
<div>
<img src="http://placeimg.com/400/200/people"/>
</div>
<div>
<img src="http://placeimg.com/400/200/tech"/>
</div>
</div>


this is my php file and I included the script at the start of my php. but it seems the connection is being created? I am a beginner at this. Thanks

Answer

If you want to keep javascript code in your php file then wrap it into <script> </script> tag and put it at the end of your php file.

If it's a separate .js file then include it at the end of your php file: <script type="text/javascript" src="Path/of/your/.js/file"> </script>.