Some general mistakes in jQuery syntax by us

Well, in the previous post I taught and described how jQuery can be used to create dynamic pages. But at the same time there are some errors made by us, which we totally forget while writing the code.

For example the basic and the first mistake that even a professional would do is the following line

$('button').click(function () {
 // functions..
}

Above code looks well, doesn’t it? Actually this would work when you’re using an online editor such as jsfiddle.net because they don’t need to be written or provided some extra code lines.

So, what was the error? The basic and the very simple to get error was that the above code in your own app should start with a document ready function, such as

$(document).ready(function () {
  $('button').click(function () {
    // functions here..
  }
}

Which will force the jQuery to work when the document is ready. Otherwise your jQuery code won’t work.

To be noted here, there is no difference in ‘ and ” in jQuery. You can use them both and the above code would be

$(document).ready(function () {
  $("button").click(function () {
    // functions here..
  }
}

Both will work. So, if there is any other error that humans catch, please share it in comments I will update the post and also if there is error in my code please share that too! :)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s