Artur’s Blog

past, present and future in web development

Why should you use jQuery

Posted on | September 3, 2008 |

If you ask me one year ago “have you seen jQuery” my answer would be “have you seen what?”

If you ask me six months ago I would say “I heard about it but I don’t see any benefit in using it. I know javascript well enough to write my own code”.

And I was sooooo WRONG.

I just finished small project for my friend www.jardinsdesreves.pl and I decided to give it a try.
The benefit: my js file is only 170 lines long.

I just want to show you couple benefits of this wonderfull framework.

FORMS:

I have never seen a simpler way to submit forms via ajax.

Just look at the simple example:

1
2
3
4
5
6
7
<form>
  <input type="hidden" name="id" value="2">
  <input type="hidden" name="save" value="1">
  Title: <input type="text" name="form_title"><br>
  Content: <textarea name="form_content" cols="25" rows="8"></textarea>
  <input type="button" value="Save changes" onclick="sendForm('text_save.php');">
</form>
1
2
3
4
5
6
7
8
9
10
function sendForm(post_url) {
  var request = $("form").serialize();
  $.post(post_url, request, function(data) {
    formResponse(data);
  });
}
 
function formResponse(resp_data) {
  $("#message").text(resp_data);
}

As you can see jQuery does all job for us.
There is only one requirement - each form element needs to have a name (not id).

serialize() function does great job and will post all form to ‘text_save.php’ where we can do processing and show response to the user.

In the next article I will show you how to use great jQuery plugin ‘ajaxfileupload.js’ to upload files.

Comments

Leave a Reply





About

I will add soon ...

Subscribe to our feed

Search

Admin