Probably a simple solution, but i don’t get it working. I have a table with unique id attributes. Via buttons i can change the order of the table rows. When i click a button i want to receive the current order. This works fine with the code below except for the first value, which is undefined. How can i get rid of that undefined value?
$("#saveOrder").click(function(e){
var event = e || window.event;
event.preventDefault();
var t = "";
$("#eventTable tr").each(function(){
t += $(this).attr("id");
});
alert(t);
});
I tried t += $(this).attr("id").replace("undefined",""); but then the script won't alert() anything
Try
t += this.id || "";This will substitute an empty string for undefined values.Note that you can’t use
replaceonundefined, sinceundefinedisn’t a string. You can convert a valuexto a string representation viaString(x)orx+''. However, this isn’t necessary in this case.EDIT
As Esailija points out, no need to create a jQuery object here.