Why Is Everything a “Hack” With Javascript?
As I have stated in the past, I hate JavaScript. It looks messy and feels very sloppy to code. After relentlessly studying JQuery for the past several weeks I have come to the conclusion that everything in JavaScript is a Hack.
For example :
If you have ever used Wordpress, you are farmilliar with adding “Tags” to posts to help categorize them. Sometimes while writing a post you have a great idea for a tag, so you scroll down and add it, then continue to write your post without having to wait for the page to reload (delimiting the chance of losing your content).
This is made possible with a methodology of client side scripting called “AJAX”. Basically it allows you to send information to the server (Asynchronously) so no data is lost - great.
Now with all of the tools available to us developers today such as Scriptaculous and JQuery - this process is still a nightmare. I am currently working on an shopping cart application for a client and tags must be added to products for indexing and other cool stuff.
Time For a Hack
I create the form and successfully send the post data via ajax, a yellow bar flashes and the new data is appended to the shiny new div. Fortunately this only took me three hours to hack together.
Now the user wants to remove a tag? What if he accidentally misspelled the tag or just doesn’t like it any more.
Time For a Hack
Make a button or link and send the post values once again. So everything is working great but all of a sudden I can not delete a newly added tag, when I click it - it does nothing.
More Hacking! The new tag was appended to the tags div and I can delete any other pre-existing tags but this new tag just will not go away.
Unfortunately this type of issue is difficult to find any help for considering the fact that you can’t type “appended button will not delete” in Google and expect any decent results.
I am going to smash my face against my keyboard for a couple more hours …or just start drinking.
Oh heres the code that took me 7 hours to write, it still doesn’t work properly…
$(function() {
$("#tagForm").submit(function(){
var product_id;
var tag;
product_id = $("input[@name=product_id]").val();
tag = $("input[@name=tag]").val();
$.post("/products/addtag",{
product_id: product_id,
tag: tag,
},
function(post) {
$('#tags').highlightFade('lightyellow');
$('#tags').append("
"+tag+" ");
});
return false;
});
$('#tags > a').click(function() {
$(this).fadeOut('slow');
$.post("/products/deletetag",{
tag_id: (this.id),
});
return false;
});
})
I’ve always felt this way towards Javascript, as you know since we’ve discussed this many times. I think the feeling is mutual to any programmer who comes from a background of working languages like C/C++, Java, etc. I do have a respect for it though. It has its uses and makes you think differently about coding. For instance when I was working with closures and getting object oriented principles to work in Javascript it definitely made me think outside the box and test my understanding of things.
I also hate Javascript that was until i started messing with ExtJS and found a new love for it.
Im unsure as to why you had such a problem removing an element from the dom? just wrap each tag in a with a ID and then remove via the id :S
I know that its never as simple as it sounds