I just started looking at jQuery. I currently do not have AJAX in my web application.
My existing JavaScript in my HTML looks like this:
<form ...> <p>Find what? <input ...></p> <div class="ButtonArray"> <a href="AddRecord.ASP&Action=ADD">ADD</a> <a href="#" onClick="return MyFormSubmit();">FIND</a> </div> </form>
The buttons for [Add] and [Find] are displayed here. For Find, you need to submit a form, and I use MyFormSubmit (), which checks the data, adds a visual effect to the page, and then the POST data.
I could add CLASS or ID for the Find button, and in JQuery I could write something like:
$(function() { $(".MySearchButtonClass").click(function() {
At my "newbie" stage of understanding with jQuery, I don’t understand why I would do that.
My old way identifies a method next to an object. Perhaps there is a risk that JS did not load before the user clicks the button (is there? LOAD for JS is at the top of the page).
The JQuery Document Ready method will not be activated until the document is loaded (which, I believe, ensures that JS is present and ready to run?), But it moves the code to a separate part of my HTML file - so when I see MyButtonArray DIV in the HTML source It’s not obvious to me which objects have methods and which don’t.
Can you please help me understand what options and advantages / gotchas I have that I should pay attention to?
Change It’s more convenient for me to manipulate the DOM - for example, LightBox, which can appear when any thumbnail with the "LightBoxThumb" class is clicked - will use Unobtrusive Javascript.
However, I am struggling to convince myself that a button that has a specific action should apply its method in this way. (Of course, I would not put any code on a button, except for one function call on something "in another place", but, in my opinion, this is my best key to what this button does, and the layers are unobtrusive Javascript can make this much harder to define.)
Edit2 - Accepted Answer
I took Cam's answer. It describes unobtrusive Javascript in a way that was useful to me to better understand how and when to use it.
At the moment (it can change when I have more experience!) I will stick to the OnClick event for one, unchanging action on the object, as I feel that it will be easier for me to debug - and diagnose if problems arise. For ZebraStripes on a table that allows you to click on the header column for sorting (and situations of this type), I can see the benefits of an unobtrusive Javascript approach
The last comment of Russia was especially useful, repeated here:
"@ Kristen - You're right, as there are so many software topics, there is more than one approach, and people will vehemently support their beliefs! If we talk about one method for one button I will fully understand where you came from ...
If we are talking about many JavaScripts with the same function calls for more than one element, different function calls for different methods, etc. I think it will be more difficult for myself to mix embedded and unobtrusive approaches, and it would be better to take one or the other approach "