Rectangle 27 28

When you attach an event handler to a DOM element, it stays intact. The class is just a way to reference the element, and changing the class will not unbind the event handlers, for that you will have to manually unbind the handler, and if using jQuery 1.7+ on() and off() is the way to go :

$('.clickable').on('click', function() {
  $(this).removeClass('clickable').addClass('not-clickable').off('click');
});

this would make the element clickable only once, and you could just use the built in one() function instead, as that will unbind the handler automagically after the first click :

$('.clickable').one('click', function() {
  $(this).removeClass('clickable').addClass('not-clickable');
});

Thanks for the explanation - I can see what happens now.

javascript - jQuery click() still being triggered after .clickable cla...

javascript jquery
Rectangle 27 4

People have already posted examples that are good options for detection, but based on your requirement of "give warning that the site is not able to function properly without the browser having JS enabled". You basically add an element that appears somehow on the page, for example the 'pop-ups' on Stack Overflow when you earn a badge, with an appropriate message, then remove this with some Javascript that runs as soon as the page is loaded (and I mean the DOM, not the whole page).

html - How to detect if JavaScript is disabled? - Stack Overflow

javascript html code-snippets
Rectangle 27 4

People have already posted examples that are good options for detection, but based on your requirement of "give warning that the site is not able to function properly without the browser having JS enabled". You basically add an element that appears somehow on the page, for example the 'pop-ups' on Stack Overflow when you earn a badge, with an appropriate message, then remove this with some Javascript that runs as soon as the page is loaded (and I mean the DOM, not the whole page).

People have already posted examples that are good options for detection, but based on your requirement of "give warning that the site is not able to function properly without the browser having JS enabled". You basically add an element that appears somehow on the page, for example the 'pop-ups' on Stack Overflow when you earn a badge, with an appropriate message, then remove this with some Javascript that runs as soon as the page is loaded (and I mean the DOM, not the whole page).

html - How to detect if JavaScript is disabled? - Stack Overflow

javascript html code-snippets
Rectangle 27 4

<style>
     tr:nth-of-type(even) {
        background-color:#e3e3e3;
     }

     td:nth-of-type(odd) {
        color:#d04242;
     }
  </style>

javascript - Alternate table row color even if row is removed - Stack ...

javascript jquery tablerow
Rectangle 27 3

You can use this

$(document.body).delegate('.clickable', 'click', function(e){
    $(this).removeClass('clickable');
    alert('Clicked!');
});

From jQuery version 1.7 delegate() is superseded by the on()

$(document.body).on('click', '.clickable', function(e){
    $(this).removeClass('clickable');
    alert('Clicked!');
});
$('.clickable').on('click', function(e){
    $(this).removeClass('clickable').off('click');
    alert('Clicked!');
});

Also you can use method one() - it's equal to bind, but occurs once

$('.clickable').one('click', function(e){
    alert('Clicked!');
});

javascript - jQuery click() still being triggered after .clickable cla...

javascript jquery
Rectangle 27 3

$(document).on('click', '.clickable', function() {
  $(this).removeClass('clickable');
  $(this).addClass('not-clickable');
  alert('Clicked!');
});

javascript - jQuery click() still being triggered after .clickable cla...

javascript jquery
Rectangle 27 3

You need to change the class as well

function update_rows() {
    $("tr:even").css("background-color", "#aaa").find('a').removeClass('sam').addClass('sams');
    $("tr:odd").css("background-color", "#eee").find('a').removeClass('sams').addClass('sam');
}
tr:nth-child(odd) a {
    background-color:#FF00FF;
}
tr:nth-child(even) a {
    background-color:#0000FF;
}
tr:nth-child(odd) {
    background-color:#aaa;
}
tr:nth-child(even) {
    background-color:#eee;
}
$(function () {
    $("a").click(function () {
        $(this).closest('tr').remove();
    });
});
$(this).closest('tr').remove()
$(this).parent().parent().remove()

Thanks man.you saved me.This is what i am looking for.

javascript - Alternate table row color even if row is removed - Stack ...

javascript jquery tablerow
Rectangle 27 188

Remove ALL instances from an array

A friend was having issues in InternetExplorer8, and showed me what he did. I told him it was wrong, and he told me he got the answer here. The current top answer will not work in all browsers (InternetExplorer8 for example), and it will only remove the first occurrence of the item.

function remove(arr, item) {
      for(var i = arr.length; i--;) {
          if(arr[i] === item) {
              arr.splice(i, 1);
          }
      }
  }

It loops through the array backwards (since indices and length will change as items are removed) and removes the item if it's found. It works in all browsers.

@sroes it should not be because the loop starts at i = arr.length -1 or i-- making it same as the max index. arr.length is just an initial value for i. i-- will always be truthy (and reducing by 1 at each loop op) until it equals 0 (a falsy value) and the loop will then stop.

Second function is rather inefficient. On every iteration "indexOf" will start search from beginning of array.

@AmberdeBlack, you're exactly right. Surprised I didn't catch that when I answered: jsperf.com/remove-all-from-array

@AlJey, it's available only from IE9+. There is still a chance that it wouldn't work.

This answer worked for me because I needed several items removed but not in any particular order. The backwards progression of the for loop here handles removing items from the array perfectly.

How do I remove a particular element from an array in JavaScript? - St...

javascript arrays
Rectangle 27 188

Remove ALL instances from an array

A friend was having issues in InternetExplorer8, and showed me what he did. I told him it was wrong, and he told me he got the answer here. The current top answer will not work in all browsers (InternetExplorer8 for example), and it will only remove the first occurrence of the item.

function remove(arr, item) {
      for(var i = arr.length; i--;) {
          if(arr[i] === item) {
              arr.splice(i, 1);
          }
      }
  }

It loops through the array backwards (since indices and length will change as items are removed) and removes the item if it's found. It works in all browsers.

@sroes it should not be because the loop starts at i = arr.length -1 or i-- making it same as the max index. arr.length is just an initial value for i. i-- will always be truthy (and reducing by 1 at each loop op) until it equals 0 (a falsy value) and the loop will then stop.

Second function is rather inefficient. On every iteration "indexOf" will start search from beginning of array.

@AmberdeBlack, you're exactly right. Surprised I didn't catch that when I answered: jsperf.com/remove-all-from-array

@AlJey, it's available only from IE9+. There is still a chance that it wouldn't work.

This answer worked for me because I needed several items removed but not in any particular order. The backwards progression of the for loop here handles removing items from the array perfectly.

How do I remove a particular element from an array in JavaScript? - St...

javascript arrays
Rectangle 27 187

Remove ALL instances from an array

A friend was having issues in InternetExplorer8, and showed me what he did. I told him it was wrong, and he told me he got the answer here. The current top answer will not work in all browsers (InternetExplorer8 for example), and it will only remove the first occurrence of the item.

function remove(arr, item) {
      for(var i = arr.length; i--;) {
          if(arr[i] === item) {
              arr.splice(i, 1);
          }
      }
  }

It loops through the array backwards (since indices and length will change as items are removed) and removes the item if it's found. It works in all browsers.

@sroes it should not be because the loop starts at i = arr.length -1 or i-- making it same as the max index. arr.length is just an initial value for i. i-- will always be truthy (and reducing by 1 at each loop op) until it equals 0 (a falsy value) and the loop will then stop.

Second function is rather inefficient. On every iteration "indexOf" will start search from beginning of array.

@AmberdeBlack, you're exactly right. Surprised I didn't catch that when I answered: jsperf.com/remove-all-from-array

@AlJey, it's available only from IE9+. There is still a chance that it wouldn't work.

This answer worked for me because I needed several items removed but not in any particular order. The backwards progression of the for loop here handles removing items from the array perfectly.

How do I remove a particular element from an array in JavaScript? - St...

javascript arrays
Rectangle 27 2

$('.clickable').live('click',function() {
  $(this).removeClass('clickable');//remove the class
  $(this).unbind('click');//to remove the click event
  $(this).addClass('not-clickable');
  alert('Clicked!');
});

javascript - jQuery click() still being triggered after .clickable cla...

javascript jquery
Rectangle 27 2

<table id="whatever">
  <tr><td>Row 1</td><td><a href="#">Delete</a></td></tr>
  <tr><td>Row 2</td><td><a href="#">Delete</a></td></tr>
  <tr><td>Row 3</td><td><a href="#">Delete</a></td></tr>
  <tr><td>Row 4</td><td><a href="#">Delete</a></td></tr>
  <tr><td>Row 5</td><td><a href="#">Delete</a></td></tr>
</table>
<style>
#whatever tr {
    background-color: #AAA;
}
#whatever tr a {
    background-color:#F0F;
}
#whatever tr:nth-child(odd) {
    background-color: #EEE;
}
#whatever tr:nth-child(odd) a {
    background-color:#00F;
}
</style>
<script>
$("#whatever a").click(function(){
    $(this).closest("tr").remove();
});
</script>

Now no manual updating, neither for the link classes nor the row backgrounds is needed. See updated demo.

javascript - Alternate table row color even if row is removed - Stack ...

javascript jquery tablerow
Rectangle 27 92

It's what the DOM supports. Search that page for "remove" or "delete" and removeChild is the only one that removes a node.

I'm just guessing here, But I would assume it has to do with memory management. The parent node most likely holds a list of pointers to the child nodes. If you just deleted a node (without using parent), the parent would still hold the pointer and cause a memory leak. So the api forces you to call a function on the parent to delete the child. this also is nice because it can walk the tree down through the child nodes calling remove on each of them, and not leaking memory.

hey guys, even though that reference doesn't have this, i found it accidentally. writing element.remove(); will work. Maybe it's something new. But first time for me and it works. I think it should have worked always as it's very basic must have stuff.

But it does not work in IE7 and below. From IE7 and below, remove() does not work

If I had to guess, the reason it works like this is that DOM elements can't remove themselves. You're removing the proverbial carpet from under it's feet. You have to remove it from the container. At least that's how I try to think of it.

javascript - Remove element by id - Stack Overflow

javascript
Rectangle 27 90

It's what the DOM supports. Search that page for "remove" or "delete" and removeChild is the only one that removes a node.

I'm just guessing here, But I would assume it has to do with memory management. The parent node most likely holds a list of pointers to the child nodes. If you just deleted a node (without using parent), the parent would still hold the pointer and cause a memory leak. So the api forces you to call a function on the parent to delete the child. this also is nice because it can walk the tree down through the child nodes calling remove on each of them, and not leaking memory.

hey guys, even though that reference doesn't have this, i found it accidentally. writing element.remove(); will work. Maybe it's something new. But first time for me and it works. I think it should have worked always as it's very basic must have stuff.

But it does not work in IE7 and below. From IE7 and below, remove() does not work

If I had to guess, the reason it works like this is that DOM elements can't remove themselves. You're removing the proverbial carpet from under it's feet. You have to remove it from the container. At least that's how I try to think of it.

javascript - Remove element by id - Stack Overflow

javascript
Rectangle 27 90

It's what the DOM supports. Search that page for "remove" or "delete" and removeChild is the only one that removes a node.

I'm just guessing here, But I would assume it has to do with memory management. The parent node most likely holds a list of pointers to the child nodes. If you just deleted a node (without using parent), the parent would still hold the pointer and cause a memory leak. So the api forces you to call a function on the parent to delete the child. this also is nice because it can walk the tree down through the child nodes calling remove on each of them, and not leaking memory.

hey guys, even though that reference doesn't have this, i found it accidentally. writing element.remove(); will work. Maybe it's something new. But first time for me and it works. I think it should have worked always as it's very basic must have stuff.

But it does not work in IE7 and below. From IE7 and below, remove() does not work

If I had to guess, the reason it works like this is that DOM elements can't remove themselves. You're removing the proverbial carpet from under it's feet. You have to remove it from the container. At least that's how I try to think of it.

javascript - Remove element by id - Stack Overflow

javascript
Rectangle 27 3

You should add an even listener to the marker that is fired when the marker is dropped.

// adds an event listener on the marker. 
// The event is fired when the marker is dropped in this case
google.maps.event.addListener(marker, 'dragend', function() {
    alert('Marker dropped');
});
draggable:true

Here is the doc for the methods and events for the Marker class: google.maps.Marker

And here a demo on jsFiddle

javascript - Update db with latitude and longitude when google map mar...

javascript php mysql google-maps
Rectangle 27 3

Your obj.json.cols is actually an array, not an object, so you should iterate over it with a for(;;) loop. I believe you're looking for something like this:

for (var i=0; i<json.cols.length; i++) {
    console.log("col name: " + json.cols[i].label);
}

Furthermore, there seems to be some script messing with Array.prototype on your webpage, as with a for..in loop you should not be seeing a remove property.

I tested here, the result is the same.

So your object is not what you think, see jsfiddle.net/T2hUX

I have an Array.prototype.remove() specified, yes. I guess that explains where it is coming from. Thanks for reminding me.

When iterating JSON with Javascript for...in, the final key returned i...

javascript json google-chrome
Rectangle 27 3

if( json.cols.hasOwnProperty(c) ) { ... }

If you only want to consider properties attached to the object itself, and not its prototypes, use getOwnPropertyNames or perform a hasOwnProperty check (propertyIsEnumerable can also be used).

A for...in loop iterates over the properties of an object in an arbitrary order

for (var c in json.cols) {
    if( json.cols.hasOwnProperty(c) ) {
      console.log("col name: " + c);
    }
}

If you wouldn't mind, please let me know why I was down voted. If you feel the information is incorrect then I will gladly fix it =).

Probably because there is no reason to use for...in on arrays, a normal for loop will do just fine (it's faster too).

I don't see the problem of his implementation with for...in, he used correctly the hasOwnProperty how ever, the for loop is a faster iterator. I don't think give @Chase a down vote is honest.

Yeah, I completely agree on the relevance of the for...in vs a for, however, I mainly wanted to specify what the issue actually was, instead of just saying "Change it to a for loop". Thanks though @FelixKling, you're probably correct. Since @bfavaretto already has the for loop difference, there's little reason for me to add it to my answer, however, it could be beneficial to some, so I'll leave it here for now.

When iterating JSON with Javascript for...in, the final key returned i...

javascript json google-chrome
Rectangle 27 3

Your obj.json.cols is actually an array, not an object, so you should iterate over it with a for(;;) loop. I believe you're looking for something like this:

for (var i=0; i<json.cols.length; i++) {
    console.log("col name: " + json.cols[i].label);
}

Furthermore, there seems to be some script messing with Array.prototype on your webpage, as with a for..in loop you should not be seeing a remove property.

I tested here, the result is the same.

So your object is not what you think, see jsfiddle.net/T2hUX

I have an Array.prototype.remove() specified, yes. I guess that explains where it is coming from. Thanks for reminding me.

When iterating JSON with Javascript for...in, the final key returned i...

javascript json google-chrome
Rectangle 27 3

if( json.cols.hasOwnProperty(c) ) { ... }

If you only want to consider properties attached to the object itself, and not its prototypes, use getOwnPropertyNames or perform a hasOwnProperty check (propertyIsEnumerable can also be used).

A for...in loop iterates over the properties of an object in an arbitrary order

for (var c in json.cols) {
    if( json.cols.hasOwnProperty(c) ) {
      console.log("col name: " + c);
    }
}

If you wouldn't mind, please let me know why I was down voted. If you feel the information is incorrect then I will gladly fix it =).

Probably because there is no reason to use for...in on arrays, a normal for loop will do just fine (it's faster too).

I don't see the problem of his implementation with for...in, he used correctly the hasOwnProperty how ever, the for loop is a faster iterator. I don't think give @Chase a down vote is honest.

Yeah, I completely agree on the relevance of the for...in vs a for, however, I mainly wanted to specify what the issue actually was, instead of just saying "Change it to a for loop". Thanks though @FelixKling, you're probably correct. Since @bfavaretto already has the for loop difference, there's little reason for me to add it to my answer, however, it could be beneficial to some, so I'll leave it here for now.

When iterating JSON with Javascript for...in, the final key returned i...

javascript json google-chrome