Onclick doesn't work worth audio.js - php

I am troubleshooting a website that uses audio.js as an audio player. I am trying to configure the audio player's event callbacks using the click method, but it is not recognizing my click.
It does however work like it should on this jsfiddle. Any help is greatly appreciated
What I've done -
Eliminated all CSS and irrelevant scripts.
Click method works on divs before and after the audiojs class.
Shortcode that I'm using for the audio player -
// audio player
add_shortcode( 'sp_audio', 'sp_audio_shortcode' );
function sp_audio_shortcode( $atts, $contents = null )
{
extract( shortcode_atts( array(
'id' => 'sp-audio-player',
'url' => '',
),
$atts ) );
$output .= '<audio preload="none">';
$output .= '<source src="' . $url . '" type="audio/mpeg">';
$output .= '</audio>';
return $output;
}
Output in Chrome's inspect element
<div class="musicplayer">
<div class="audiojs" classname="audiojs" id="audiojs_wrapper5">
<audio preload="none">
<source src="http://soundtrackloops.com/Demo/Electronisounds-AcousticBarefootBeats-DEMO.mp3" type="audio/mpeg">
</audio>
<div class="play-pause">
<p class="play"></p>
<p class="pause"></p>
<p class="loading"></p>
<p class="error"></p>
</div>
<div class="scrubber">
<div class="progress"></div>
<div class="loaded"></div>
</div>
<div class="time">
<em class="played">00:00</em>/<strong class="duration">00:00</strong>
</div>
<div class="error-message"></div>
</div>
My test click method
jQuery(document).ready(function($) {
$( ".audiojs" ).click(function() {
alert( "CLICK" );
});
Initialize audio.js
audiojs.events.ready(function() {
audiojs.createAll();
});

Looks like you're appending the audio track after the page has loaded so you'll need to use a live click event.
Try this:
jQuery(document).ready(function($) {
$('.audiojs').live('click', function() {
alert( "CLICK" );
});

I had a similar issue and solved it by using event listeners on the audio object. I guess it depends on what you want to track but that gives you control over playing and stopping etc.
I adapted my code so it should be working with yours
audiojs.events.ready(function() {
var as = audiojs.createAll();
$(".audiojs audio").on('playing',function(){
console.log("Push playing");
});
// Other callbacks
});
Hope you get it working. I only have one player per page, if you have more just use the $(this) element in the callback to identify which has been altered.

Related

How To trigger play button of flow player

I have integrated flowplayer in my website.
As a next step, I want to trigger its video play function, so that when a user plays a video, she/he will simply press a play button, that will call a function.
Can anybody please help me achiving that?
I am trying to do this:
<script language="javascript">
$(document).ready(function(){
$( ".fp-play" ).change(function() {
alert( "Handler for .click() called." );
});
});
</script>
This is my HTML code:
<div class="artist_vidoe_player fl clear">
<div class="flowplayer" data-swf="<?php echo WEB_URL; ?>assets/styles/frontend/js/flowplayer.swf" data-ratio="0.4167">
<video>
<source type="video/flv" src="<?php echo WEB_URL; ?>assets/styles/uploads/videos/<?php echo $rowvid->v_uploadname; ?>">
</video>
</div>
<div style="float:left; margin-top:15px;">
<div class="clear"></div>
<h1><?php echo $rowvid->v_filename; ?></h1>
<p>258 Views</p>
<p><img src="<?php echo WEB_URL; ?>assets/styles/frontend/images/stars_img.png" width="90" height="16" alt="" /></p>
</div>
</div>
The above code dynamically generates my videos, but this is all not working for me anyway.
You can use .click() instead of .change() as well as using .trigger() to trigger click event:
$(document).ready(function(){
$( ".fp-play" ).click(function() {
alert( "Handler for .click() called." );
}).trigger('click');
});

Jquery Tools Overlay: php foreach loop not working

I put Jquery Tools's Overlay in my site to show a projects' info in several overlays. This works pretty ok, but I have been trying to 'automate' the code to read new projects and load them in overlays. What happen looks ok, but no matter which project I click, the overlays allways load the content of the first project...
I did a lot of googling around and copy-pasting to get this far, I am not (yet) much of a programmer, I hope the code doesn't scare you guys.. ;-)
Anyway, here's a link: http://www.wgwd.nl/test
If you click 'Projects' a normal div opens that loads all the projects it finds (two, for now). When you click one it opens that content in 3 overlays. As said, unfortunately it allways loads the same content independent of which project you click.
I have tried to assign the JScript a unique function name (generated with php from the project's filename) but that doesn't seem to work.
Any ideas? here's my code :
<?
//reads projectfolder and distills
//a basename out of the project description.txt
$textfiles = glob('content/projects/*.txt', GLOB_BRACE);
foreach ($textfiles as $textfile) { ?>
<div id="details"> <?
$pad = pathinfo ($textfile);
$base_name = basename($textfile,'.'.$pad['extension']);
// the link that opens the overlays. Don't think the "id" tag is nescessary
echo '<a id="'.$base_name.'" href="#" onclick="'.$base_name.'()"><img src="'.$base_name.'/main.jpg"/></a>' ?>
<!-- defines overlay, hidden by default -->
<div id="dragwindow1" class="overlay ol1">
<a class="close"></a>
<?
include ('content/projects/'.$base_name.'/content.txt');
?>
</div>
</div>
<?
// the description of each project
include ($textfile);
?>
<script>
// within the foreach open all overlays with function name $base_name
var <?=$base_name?> = function () {
$("a[rel]").each(function() {
$(this).overlay().load();
});
}
</script>
<hr />
<? } //end foreach ?>
</div>
<!-- somehow, without defining these links, the whole 'open all overlay' thing doesn't work -->
<a rel="div.overlay:eq(0)" type="button" style="display: none">first</an>
<a rel="div.overlay:eq(1)" type="button" style="display: none">second</a>
<a rel="div.overlay:eq(2)" type="button" style="display: none">third</a>
<script type="text/javascript">
$(function projects() {
// positions for each overlay
var positions = [
[120, '15%'], //uppper left, #1
[70, '60%'], // lower left, #2
['60%', '40%'], // lower right, #3
];
// setup triggers
$("a[rel]").each(function(i) {
$(this).overlay({
// common configuration for each overlay
oneInstance: false,
// setup custom finish position
top: positions[i][0],
left: positions[i][1],
});
});
});
</script>
Thx in advance!
EDIT: I edited the code to omit all that's unrelated
The question remains: Javascript only returns the content of the first call in the foreach loop. Is there anyway to generate multiple instances of the javascript for each loop in the PHP?
SOLVED! With big, big, help of a friend, who redefined how multiple Overlays from Jquery Tools could work (and should have worked in the first place...)
Without getting too much into it, here's the code for future reference:
Basically the trick is:
// open all overlays
function openAll(currentOverlays) {
$(currentOverlays).each(function()
{
$(this).overlay().load();
});
}
The complete page is now something like this:
<script type="text/javascript">
$(function () {
// positions for each overlay
var positions = [
['60%', 540], // lower right, #3
[80, '65%'], // lower left, #2
[120, '12%'], //uppper right, #1
];
// setup triggers
$("div.overlay").each(function(i) {
$(this).overlay({
// some configuration for each overlay
// positioning the overlays
top: positions[i % 3][0],
left: positions[i % 3][1]
});
});
});
// open all overlays
function openAll(currentOverlays) {
$(currentOverlays).each(function()
{
$(this).overlay().load();
});
}
// close all overlays
function closeAll(currentOverlays) {
$(currentOverlays).each(function()
{
$(this).overlay().close();
});
}
</script>
<div id="projectstarter">
<h2>Projects</h2>
<div class="maindetails">
<a class="close"></a> <!-- defines a close button for the overlay -->
<?
$textfiles = glob('content/projects/*.txt', GLOB_BRACE);
rsort($textfiles);
foreach ($textfiles as $textfile) {
$pad = pathinfo ($textfile);
$base_name = basename($textfile,'.'.$pad['extension']);
echo '<a href="#" onclick="openAll(\'div.'.$base_name.'\')">';
echo '<img src="./content/projects/'.$base_name.'/projectimage.jpg" class="thumb"/></a></div>';
include '$textfile'; //project description
} // end MAIN foreach ?>
</div>
</div>
<div id="projects">
<?
foreach ($textfiles as $textfile) {
$pad = pathinfo ($textfile);
$base_name = basename($textfile,'.'.$pad['extension']); ?>
<div id="dragwindow3" class="<?=$base_name?> overlay ol3">
<a class="close"></a>
<h2>Media</h2>
<div class="details">
// include media here
</div>
</div>
<div id="dragwindow2" class="<?=$base_name?> overlay ol2">
<a class="close"></a>
<h2>Credits</h2>
<div class="details">
// include credits here
</div>
</div>
<div id="dragwindow1" class="<?=$base_name?> overlay ol1 ">
<a class="close"></a>
<h2>Content</h2>
<div class="details">
// include content here
</div>
</div>
<? } ?>
<script>
$( "#projectstarter" ).overlay();
$( "#projectstarter" ).draggable().resizable({ghost: true});
$( ".ol1" ).draggable().resizable({ghost: true});
$( ".ol2" ).draggable().resizable({ghost: true});
$( ".ol3" ).draggable().resizable({ghost: true});
</script>

PHP in jQuery tab

I want to create button to add tabs with body tabs
Please take look at the code where it says
$('#tabs1').tabs('add', 'lorem.txt', xxxxx' .....
as you can see 'lorem.txt' is from where the information is retrieved to show in the body tabs,
I want to put php code and forms inside tabs body and not use 'lorem.txt'
The problem is that it will not work if I put php code inside jquery.
My question is, is there any other way it can done to show forms using php code when person click button to add tabs?
I'm using div tag. Here is my code.
<script>
(function() {
$(function() {
var tab_counter = 0;
$('#tabs1').tabs({
closable: true,
cache:true,
add: function(e, ui){
$('#tabs1').tabs('select', '#' + ui.panel.id);
}
});
$('#tab_creator').click(function(){
tab_counter += 1;
$('#tabs1').tabs('add', 'lorem.txt', 'Tab ' + tab_counter);
return false;
});
$('#tabs2').tabs();
});
})(jQuery);
</script>
</head>
<body>
<h2 class="ui-widget ui-widget-header">closable Tabs</h2>
<input id="tab_creator" type="button" value="Add New Tab">
<div id="tabs1">
<ul>
<li>tabs one</li>
<li>tabs two</li>
<li>tabs three</li>
</ul>
<div id="tabs1-1">
<p> tabs one body</p>
</div>
<div id="tabs1-2">
<p>tabs two body</p>
</div>
<div id="tabs1-3">
<p>tabs three body</p>
</div>
</div>
<br>
<br>
If the JS code is placed directly at <head> (as your example):
$('#tabs1').tabs('add', '<?php echo $filename; ?>', 'Tab ' + tab_counter);
Otherwise, you need something like this:
$('#tabs1').tabs('add', $( '#tabs1' ).attr( 'data-filename' ), 'Tab ' + tab_counter);
... where you put the PHP code inside the data-filename attribute in #tabs1:
<div id="tabs1" data-filename="<?php echo $filename; ?>">
About data attribute: Google

jQuery UI picker with input box added using jQuery append

I am printing a form in a row on a page. The row contains a few columns which hold the form elements. Much like a table but in CSS.
When the user clicks on a link with the ID add_transaction_row then a little bit of jQuery is called which appends another row/div to the page. This row contains an identical set of div's and form elements as the row above.
I have named the elements date[], sub_cat_id[] etc so that I get an array of the updates to process on the next page.
I am using the jQuery datepicker for the date input box. I have a trigger on on class called .datepicker.
On the first row the jQuery date picker appears as expected. However when I click into any of the date input boxes on the rows below (the new rows/input box's added by jQuery append) then the datepicker does not work.
I was thinking that it might be something to do with me naming them all the same so I have given them all a unique ID. Still no luck.
I am now thinking its something to do with jQuery adding the .datepicker on page load and then when I click the bottom to add a new row this is adding the html to the source but after jQuery datepicker has loaded so it does not pick the new date/input box's up?
If anyone could shed any light on this I would appreciate it.
<?php
$my_new_split = '';
$my_new_split .= '<div class="split_transaction_box">';
$my_new_split .= '<div class="span-7">' . form_input('date[]', '', 'id="' . random_string('numeric', 8) . '" class="datepicker"') . '</div>';
$my_new_split .= '<div class="span-7">' . form_dropdown('sub_cat_id[]', $subcategories_options) . '</div>';
$my_new_split .= '<div class="span-5">' . form_input('reference[]') . '</div>';
$my_new_split .= '<div class="span-4 last">' . form_input('ammount[]') . '</div>';
$my_new_split .= '<div class="clearfix"></div></div><!-- /.split_transaction_box -->';
$my_new_split = trim($my_new_split);
$replace_this = array("\r\n", "\n", "\r");
$my_new_split = str_replace($replace_this, '', $my_new_split);
$my_new_split = str_replace('"', '\"', $my_new_split);
?>
<script>
$(document).ready(function() {
$('#add_transaction_row').click(function() {
$(".split_continer").append("<?=$my_new_split;?>");
return false;
});
});
</script>
<hr />
<div class="span-24 last">
<?=form_open('accounts/do_split')?>
<div class="split_continer">
<div class="split_transaction_box">
<div class="span-7"><?=form_input('date[]', '', 'id="' . random_string('numeric', 8) . '" class="datepicker"');?></div>
<div class="span-7"><?=form_dropdown('sub_cat_id[]', $subcategories_options);?></div>
<div class="span-5"><?=form_input('reference[]');?></div>
<div class="span-4 last"><?=form_input('ammount[]');?></div>
<div class="clearfix"></div>
</div><!-- /.split_transaction_box -->
</div><!-- /.split_container -->
<br />
Add new split
<input type="submit" class="button white" value="Save this split" />
<?=form_close()?>
Your exactly right, the datepicker does not work on new elements because you are adding those elements into the page after jquery has attached the datepicker. I've come across this issue myself. I came up with a few ways of doing it.
The first way was to bind a live click handler to the element, check if datepicker is applied, and if not apply it.
http://jsfiddle.net/uyx67/1/
But maybe a better way is to build the elements using jquery as objects and apply the datepicker that way
http://jsfiddle.net/4jmkw/3/
Both ways work, and do the job.
<script>
$(document).ready(function() {
$('#add_transaction_row').click(function() {
var new_row = jQuery("<?=$my_new_split;?>");
$(".split_continer").append(new_row);
$( ".datepicker" ).datepicker({
numberOfMonths: 2,
dateFormat: 'DD, d MM, yy'
});
return false;
});
});
</script>

Multiple instances of SlideToggle which toggle everything

I have the following script which appears multiple times on my page. I have a simple slide toggle attached to <div class="info"> and contolled by <a rel="viewinfo"> tag, but when I click on the anchor, all the divs with class info slide.
here is the HTML/PHP
<div class="couplistMeta">
<a class='view' rel="viewinfo" href="#">View Coupon</a>
<a class="print" href="#">Print</a>
</div>
<div class="info">
<p><?php echo $rec2[4]." -- Phone: ".$rec2['phone']; ?></p><p><?php echo $rec2['address']." -- ".$rec2['city'].", ".$rec2['state']." ".$rec2['zip']; ?></p>
</div>
Here is the Javascript
$(document).ready(function() {
$('div.info').hide();
$("a[rel='viewinfo']").click(function() {
$('div.info').slideToggle(400);
return false;
});
});
I tried using $(this).next('div.info').slideToggle(400); but this just broke the effect. So $('div.info') is too general, how can I be more specific with the JS?
Try this:
$(document).ready(function() {
$('div.info').hide();
$("a[rel='viewinfo']").click(function() {
$(this).parent().next('.info').slideToggle(400);
return false;
});
});
The issue being due to $('div.info') implicitly iterating over all div elements with the class info on the page.

Categories