Hi everyone i'm searching for a little help.
I have a php page, in this page i have some ajax calls that return a divs...
in one of this div i have a button, if this button will click i have to remove the entire div where button is..
here's my code, not working now :(
$(document).ready(function() {
$("#tutn").live('click',function(e){
$("#first_time",e).remove() ;
e.preventDefault() ;
});});
Where id="first_time" is a div called with ajax... and this is my problem .. because the div doen't remove even if i click on the button....
In others world i have to remove the father of the div where button is
this is the html code
<div id="first_time">
<div class="ft_info">
<input type="button" name="tutn" id="tutn" class="tutbtn" value="no" />
</div>
</div>
If you're certain the div exists and its ID is first_time, just do
$('#first_time').remove();
The second parameter is supposed to be an existing DOM node, the starting point where jQuery will begin selecting elements. You appear to be passing the event object, which isn't what jQuery is expecting.
Furthermore, there is no need to pass a second parameter to an ID selector, as the ID must be unique within the document.
A final note on style: Don't put spaces before semi-colons, and don't "save space" by nesting multiple closing }) pairs. This is gross:
$(document).ready(function() {
$("#tutn").live('click',function(e){
$("#first_time",e).remove() ;
e.preventDefault() ;
});});
There are certain well excepted coding standards that you must adopt if you want to be taken seriously:
$(document).ready(function () {
$("#tutn").live('click', function (e) {
$("#first_time",e).remove();
e.preventDefault();
});
});
You can't select the div#first_time from the context e.
Just change $("#first_time",e).remove() ; to $("#first_time").remove() ;
Just one small change should fix it...
$(document).ready(function() {
$("#tutn").live('click',function(e){
$("#first_time").remove() ;
e.preventDefault() ;
});
});
I just removed the ,e from the remove selector as it's wrong.
Your mistake is in line 3:
$("#first_time").remove() ;
The e is wrong there. I wonder that you do not get an js-error.
but i also would recommend to use parent().
$(document).ready(function() {
$("#tutn").live('click',function(e){
$(this).parent().parent().remove();
});
});
You have to use parent() two times to get $('#first_time').
And I would not use ID's. I would solve the problem with classes.
As another option, you can remove the button's "grandparent" with two chained calls to the parent() method on $(this), where this is the button element.
$(document).ready(function() {
$("#tutn").live('click',function(e){
e.preventDefault() ;
$(this).parent().parent().remove();
});});
If you want to remove the div with id first_time you can do:
$(document).ready(function () {
$("#tutn").live('click', function (e) {
$("#first_time").remove();
});
});
But if the id is a dynamically changing one then you can do:
$(document).ready(function () {
$("#tutn").live('click', function (e) {
$(this).parent().parent().remove();
});
});
Related
<a href='#' onClick ='backcolor();' class='pagecolor' id='firstpage-right-pagecolor' >
<img src='". Yii::app()->baseURL.'/images/color.png'."'></a>
here i call backcolor function (important-Its in controller) and its definition is in view as follows..
function backcolor() {
$('.pagecolor').colpick({
onSubmit:function(hsb,hex,rgb,el){
var divid = $(el).closest('div').attr('id');
$('#'+divid).css('background-color', '#'+hex);
$(el).colpickHide();
$.ajax({
url:baseURL+'/index.php/MyPhotoBooks/addbackground',
type:'POST',
data:{color:hex,divid:divid},
success:function(){
//$("#"+divid).css('height', '80%');
},
});
}
});
};
It works on second click only..for first click color pick not displaying..
This is entirely expected behaviour.
You seem to be binding the colorpick to the element on click of the same element. You need to do that on page load instead.
Recommended Solution:
Change onClick to onload. This will make sure that when your element is loaded, the colorpicker is binded to it. Next when the colorpicker element is clicked, it should work.
<a href='#' onClick ='backcolor();' class='pagecolor' id='firstpage-right-pagecolor' >
to
<a href='#' onload ='backcolor();' class='pagecolor' id='firstpage-right-pagecolor' >
-----Alternative Approach---------
Showing the colorpick on click of the element is automatically handled by the plugin. So simply change your function code to
$(function(){
$('.pagecolor').colpick({
onSubmit:function(hsb,hex,rgb,el){
var divid = $(el).closest('div').attr('id');
$('#'+divid).css('background-color', '#'+hex);
$(el).colpickHide();
$.ajax({
url:baseURL+'/index.php/MyPhotoBooks/addbackground',
type:'POST',
data:{color:hex,divid:divid},
success:function(){
//$("#"+divid).css('height', '80%');
},
});
}
});
});
This will ensure binding on page load. Also make sure to remove onclick handler from your .pagecolor element.
I am looking for a code, which I can put inside a standard HTML href link - that, once the link is clicked, will immediately update a portion of the link in real-time.
Something like:
Example
Will at first print:
Example
And so after it being clicked once, it'll instantly change to:
Example
Anyone got anything simple up the sleeve for that?
Thanks!
First include JQuery
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js" ></script>
Give your link an id
<a id="link" href="http://example.com/page1/">Example</a>
Use the link in your JQuery code
$(document).ready(function() {
$(document).on("click", "#link", function() {
$(this).attr("href", "http://example.com/page1-clicked/");
});
});
edit
I you want to do the same with many links give your link a class
<a class="link" href="http://example.com/page1/">Example</a>
Then change the JQuery code to
$(document).ready(function() {
$(document).on("click", ".link", function() {
$(this).attr("href", "http://example.com/page1-clicked/");
});
});
using JQuery's one event handler to change the link only once:
$(function () {
$('a').one(function (event) {
var elem = $(this);
var before = elem.attr('href');
var after = before + '-clicked/';
elem.attr('href', after);
})
});
I'm trying to use JQuery load() instead of javascript ajax. I have multiple of ids generated for each DIV with php loop.
When creating a JQuery function like below I pass the generated ids of divs fine but, I will have to click twice on the button in-order the .click() event to trigger , I put a window.alert() inside the .click() function and it popups twice after the second click on the button, any idea how to do it the correct way?
UPDATED
function updateDivs(generatedID){
$('#'+generatedID).click(function(){
//do some .load() stuff
});
}
<?php
for($i=0 ; $i < 20 ; $i++){
$gen = mt_rand(100,999);
?>
<div id="<?=$gen;?>" onclick="updateDivs(<?=$gen;?>) > some content </div
<?php
}
?>
I think that the easiest way is to set a class for your divs and inside you can get the div id with event.target.id.
I think that your problem is that you need to have something like document.ready() or $(function() { });
<div id="div_id" class="mydiv"> some content </div>
<script type="text/javascript">
$(document).ready(function () {
$(".mydiv").click(function (event) {
alert(event.target.id);
});
});
</script>
I think what you want is binding a doubleclick event handler, which can be done this way:
$("<?=$gen;?>").on("dblclick", function (e) {
// do some stuff here.
});
I have very limited knowledge with scripts so I hope you guys can help me with a simple solution to a small problem that I have...
I'm using the following jquery function to refresh a div with new content when a link is clicked
<script>
$(function() {
$("#myButton").click(function() {
$("#loaddiv").fadeOut('slow').load("reload.php").fadeIn("slow");
});
});
</script>
My problem is, I need to send 2 variables to the reload.php page to use in a mysql query (I have no idea how to accomplish that), also I need to make multiple links work with this function, at the moment I have multiples links with the same id and only the first link works so I guess I must associate different ids to the function in order for this to work, how can I do that?
here's the page where i'm using this: http://www.emulegion.info/teste/games/game.php
You may want to use document ready instead of function on your first line as this will make sure the code is not executed until the full page (and all elements) have loaded.
You can then use the callback functions of the fade and load to perform actions in a timely manner.
additional variables you can add after the .php, these can then be read in your reload.php file as $var1 = $_GET['var1'];
Do make sure to sanitize these though for security.
<script type="text/javascript">
// execute when document is ready
$(document).ready(function() {
// add click handler to your button
$("#myButton").click(function() {
// fade div out
$("#loaddiv").fadeOut('slow',function(){
// load new content
$("#loaddiv").load("reload.php?var1=foo&var2=bar",function(){
// content has finished loading, fade div in.
$("#loaddiv").fadeIn('slow');
}); // end load content
}); // end fade div out
}); // end add click to button
}); // end document ready
</script>
For different variables you could add a HTML5 style variable to your button.
<input type="button" id="myButton" data-var1="foo" data-var2="bar" />
You can retrieve this when the button is clicked:
// add click handler to your button
$("#myButton").click(function() {
// get vars to use
var var1 = $(this).data('var1');
var var2 = $(this).data('var2');
...
load("reload.php?var1="+var1+"&var2="+var2
if you have multiple buttons/links I would use class instead of id "myButton". that way you can apply the function to all buttons with the above script. Just replace "#myButton" for ".myButton"
First, you should use .on('click', function() or .live('click', function() to resolve your one click issue.
You'll want to do something like:
<script>
$(function() {
$("#myButton").on('click', function() {
var a = 'somthing';
var b = 'something_else';
$.post('url.php', {param1: a, param2: b}, function(data) {
//data = url.php response
if(data != '') {
$("#loaddiv").fadeOut('slow').html(data).fadeIn("slow");
}
});
});
});
</script>
Then you can just put var_dump($_POST); in url.php to find out what data is being sent.
Try creating a function that would accept parameters that you want.
Like:
$(document).ready(function(){
$('.link').click(function(){
reload(p1,p2);
});
});
function reload(param1, param2){
$("#loaddiv").fadeOut('slow').load("reload.php?param1="+param1+"¶m2="+param2).fadeIn("slow");
}
But by doing the above code your reload.php should be using $GET. Also you need to use class names for your links instead of id.
<script type="text/javascript">
// execute when document is ready
**$(document).ready(function() {**
**$("#myButton").click(function() {**
**$("#loaddiv").fadeOut('slow',function(){**
**$("#loaddiv").load("reload.php?var1=foo&var2=bar",function(){**
// content has finished loading, fade div in.
$("#loaddiv").fadeIn('slow');
});
});
});
});
</script>
$("#myButton").click(function() {
// get vars to use
var var1 = $(this).data('var1');
var var2 = $(this).data('var2');
i am trying to put a pagination page into jquery. example.html?id=foo&get=23
i would like to pass the get= into the jquery script so that i can change the page within the div instead of sending the link to the having the user see example.html?id=foo&get=23, they should only see example.html?id=foo. the rest is done in jquery.
<script >
$(document).ready(function()
{
$("#data").load("page.php?ht=<?php print $id; ?>&p=1" );
$("#next").click(function(){
var pageNum = this.id;
$("#content").load("page.php?ht=<?php print $id; ?>&p=" + pageNum, Hide_Load());
});
});
<div id="data">
page1
</div>
Why not modify the anchor
$('#next').attr("href","example.html?id=45");
After every click on the page?
you need something like this:
$(document).ready(function(){
$("#next").click(function(){
$.post('your_script_to_get_values.php',
{ page: "1" },
function(data) {
$('#content').html(data);
});
});
});
Make sure you use a live click handler, and not just a click handler, because you are creating the #next element after the page loads.
$("#next").live('click', function(){ //etc.
Documentation for .live()
It matches the current selector now and in the future (i.e. if the elements are created dynamically).
The simplest way would be with a session variable.