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>
Related
first of all i really cant get/understand the use of directive in dynamic way like after button submit then append in particular div.
in result i used jquery in appending elements in angular. my problem is ng-click is not binding on appended elements
below is my code for controller and draw function of my element
VIEW
<div class="box" ng-controller="addWorkOrder">
<div class="box-header with-border">
New Work Load
</div>
<div class="box-body">
<div class="form-group">
<input class="form-control" id="add_wl" ng-model="addwo_title" placeholder="Add Work Load">
</div>
<div class="form-group" id="add_wl_div" style="display:none">
<button type="" class="btn bg-olive pull-right" ng-click="click_addwo()">Add</button>
</div>
</div><!-- /.box-body -->
</div><!-- /.box -->
Angular Controller
app.controller('addWorkOrder',['$scope','$rootScope','$http','$compile',function($scope,$rootScope,$http,$compile){
$scope.$watch('project_id',function(){
$scope.click_addwo = function(){
$http.post(base_url+'ajax/add_workorder',{proj_id:$rootScope.project_id,wo_title:$scope.addwo_title})
.then(function(response){
if(response.data.success == 0){
alert(response.data.msg);
}else{
draw_workorder({
wo_id : response.data.data,
wo_title : $scope.addwo_title
});
$scope.addwo_title = "";
}
});
}
});
}]);
lastly my function draw_workorder() to be called upon success of submit button
function draw_workorder(data){
// workorder card view
l = '<tr data-id="'+ data.wo_id +'" style="display:none;">'+
'<td style="width:30px"></td>'+
'<td class="mailbox-name">'+ data.wo_title +'</td>'+
'<td class="mailbox-subject"><span class="badge bg-red">pending</span></td>'+
'<td class="mailbox-attachment"></td>'+
'<td class="mailbox-date" data-livestamp="'+ new Date().toLocaleString() +'"></td>'+
'</tr>';
$(l).prependTo('#wo_container').fadeIn();
}
anyone can teach me how to properly append/prepend elements using directives after http service ins controller is much appreciated
once again im sorry for being newbie in angular. i tried my best to read Angular Directive documentation and it seems other examples they give is so complicated for me.
what you need to do is to let Angular know, that your element actually has directives. This can be achieved by using $compile service, here, I simplified your example jsfiddle
app.controller('addWorkOrder', ['$scope', '$rootScope', '$http', '$compile',
function($scope, $rootScope, $http, $compile) {
$scope.click_addwo = function() {
l = '<button type="" ng-click="attachedFunct()">Click Me!</button>';
$('#wo_container').append($compile(l)($scope));
};
// Function that will be used by dynamically added element.
$scope.attachedFunct = function() {
alert('Clicked attached function');
};
}
]);
Your ng-click is not working because you need to compile the html before you append it to some element.
var $compile = ...; // injected into your code
var scope = ...;
var parent = ...; // DOM element where the compiled template can be appended
var html = '<div ng-bind="exp"></div>';
// Step 1: parse HTML into DOM element
var template = angular.element(html);
// Step 2: compile the template
var linkFn = $compile(template);
// Step 3: link the compiled template with the scope.
var element = linkFn(scope);
// Step 4: Append to DOM (optional)
parent.appendChild(element);
for more info please go to this link
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.
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
I have a conversations flow who is refresh with .load() of jQuery every 3 seconds.
This system work, but in each conversation I have an answer button who slide a form (textarea and submit button) with .toggle().
To display the flow, I use a while with PHP.
My issue is, when the is loading by .load(), and answer button is clicked, the form hide again and text contain too.
<div id="the_river_loading">
<?php
$sql_the_river = 'SELECT u.nickname, u.firstname, u.lastname, u.main_photo, u.locality,
id_conversation, id_messages, owner, participants, text, date
FROM users AS u
INNER JOIN the_river
ON u.nickname = owner
WHERE answer = 0
ORDER BY date DESC';
$result_the_river = mysqli_query($mysqli, $sql_the_river);
$count = 0;
while ($data_the_river = mysqli_fetch_assoc($result_the_river))
{
$count++;
echo '<div class="message_container_news">'; // Start block conversation
echo '<a href="/profile/' . $nickname . '" class="name_links" style="vertical-align: top; font-size: 16px;">
<img src="' . $main_photo . '" title="' . $name . '" class="members_actu_photo" />' . $name . '</a>
<span style="vertical-align: top">, ' . $locality . '</span><span style="float: right; font-size: 12px;">Posté le Octobre 25, 2012</span>
<p style="margin-top: 5px;">' . $data_the_river['text'] . '</p>
<div class="btnAnswer_nb">' . $count_answer . '</div> Answers
See conversation
<div class="btnAnswer_news" id="btnAnswer_news_id_' . $count . '">Reply</div>
<form method="post" id="display_form_id_' . $count . '" action="" style="display: none;">
<br />
<textarea name="answer_text"></textarea><br />
<input type="submit" name="answer_valid_id_' . $count . '" value="Post" />
</form>
</div>'; // End block conversation
}
?>
</div>
<script type="text/javascript">
$(".btnAnswer_news").live("click", function(){
var num_show = this.id.replace(/\D/g, "");
$("#display_form_id_" + num_show).toggle("fast");
});
var auto_refresh = setInterval(
function() {
$("#the_river_loading").load("/home" + " .message_container_news");
}, 3000
);
</script>
In pleasure of read you.
This is because if you load the content again into the container, the styles applied per js will get removed (because they were applied through style="" and this gets refreshed). You should move the Form and the Button to another Div, wich is not in the loading Div.
If I understand your problem correctly (you've not be very descriptive), you just want to not hide the form when clicking again, for that use show() instead of toggle() , so replace your following line:
$("#display_form_id_" + num_show).toggle("fast");
for this one:
$("#display_form_id_" + num_show).show("fast");
If you want to keep the text of the textarea, you can like so:
var auto_refresh = setInterval(
function() {
tx = $("#the_river_loading textarea[name='answer_text']").val();
$("#the_river_loading").load("/home" + " .message_container_news", function(){
$("#the_river_loading textarea[name='answer_text']").val(tx);
});
}, 3000
);
I've got this problem that I can't solve. Partly because I can't explain it with the right terms. I'm new to this so sorry for this clumsy question.
Below you can see an overview of my goal. I want to search some stuff from a database (with ajax) and display the results in a div below the search. This I can get this to work. But now, I want to be able to click on a result and get it in a div on top of the page.
I learned it is normal that ajax can not handle this sort of actions and it is not possible to run jquery or other ajax-calls into an ajax-call.
I also read things about jquery live() to work around this problem.
Still it is not clear to me how to make things work.
Is it possible to put me on the right track? Are there tutorials I missed?
thank you!
step one
<div id="top">here comes an image</div>
<textfield> for the ajax search string + a button </textfield>
<div id="search">target of the ajax-search</div>
step two: after a search we get this
<div id="top">here comes an image</div>
<textfield> for the ajax search string + a button </textfield>
<div id="search">result 01 with link to a php-file: a
result 02 with link to a php-file: b ... </div>
step tree: after click on result 01 or 02 ...
<div id="top">content of php-file: a</div>
<textfield> for the ajax search string + a button </textfield>
<div id="search">result 01 with link to a php-file: a
result 02 with link to a php-file: b ... </div>
ok, here comes part of the (relevant) code
search_page.php
<div id=top"></div>
<div class="client_search_field">
<form id="ui_element" class="sb_wrapper" action="" method="post" name="search_ID" ONSubmit="xmlhttpPost('search_stream_client_v02.php', 'ui_element', 'txtHint', '<img src=\'images/wait.gif\'>'); return false;">
<p> <span class="sb_down"></span>
<input name="search_string" type="text" class="sb_input" id="search_string" value="<?php echo $_POST['search_string']; ?>"/>
<button type="submit" class="submitmag" id="search_submit" > </button>
</p>
</div>
<span id="txtHint"></span>
search_stream_client_v02.php
<?php
$sql .= "SELECT *";
$sql .= ", MATCH(description, keywords) AGAINST ('".$_POST['search_string']."') AS score FROM archief WHERE MATCH(description, keywords) AGAINST('".$_POST['search_string']."' IN BOOLEAN MODE)";
$sql .= " AND showa = '1' ";
$sql .= $q_sort." ".$q_order." LIMIT 0,".$q_num_result;
$result04 = mysql_query($sql) or die(mysql_error());
while ($row04 = mysql_fetch_assoc($result04)){
$hint .= "<div class=\"tb-kader\">";
$hint .= "<span id=\"".$row['thumbnail']."\">";
$hint .= "<a href=\"".$row04['link']."\" class=\"fra tooltip lazy\" id=\"".$row04['ID']."\" TITLE=\"header=[".$row04['headline']."] body=[".$row04['description']."]\">";
$hint .= "<img src=\"$row04[thumbnail]\" border=\"0\" alt=\"".$row04['headline']."\"/></a>";
$hint .= "<div class=\"tb-kader-price\">".$pic_list_solo_final[$items03]['prijs']." €</div></span>";
$hint .= "</div>";
}
echo $hint;
?>
so, at the end the
<a href=\"".$row04['link']></a>
should open in the
<div id="top"></div>
thank you guys for your effort!!
EDIT
This one is not working:
$('#search').delegate(".fra", "click", function() {
$("#top").attr("src","href");
});
$('#search').delegate(".fra", "click", function() {
$("#top").attr("src","href");
});
What this does is just setting the scr attribute of #top to "href". That's not what you want ;) What you need is, I think:
$('#search').delegate(".fra", "click", function() {
var link = $(this),
url = link.attr("href");
$.ajax({
url: url,
dataType: "html",
success: function(data) {
$("#top").html(data);
}
})
});
so you want to do something like:
$('.search-result-class').live('click', function() {
//this binds to the click of each result. So you can do an ajax call here and on the success callback you could then load the result into the div
});
Or you can use .delegate, like this:
html
<div id="search">
result 01 with link to a php-file: a
result 02 with link to a php-file: b ...
</div>
js
// binds a click event on each result, even if they've been loaded via ajax
$('#search').delegate(".result", "click", function() {
var link = $(this),
url = link.attr("href");
// do your ajax magic...
$.ajax({url: url...})
});
It's basically the same idea than Victor's answer, but .delegate is more performant I think.