views:

52

answers:

2

So, in my example below, "InputDate'" is an input type=text, "DateColumn" is a TD within a table with a class of "DateColumn".

Read the value of a discreet texbox:

var inputVal = $('#InputDate').val();

Read the value of a div within a table....

This works:

$('#theTable .DateColumn').each(function() {
  var rowDate = Date.parse($(this)[0].innerHTML);
});

This doesn't:

$('#theTable .DateColumn').each(function() {
  var rowDate = Date.parse($(this)[0].innerHTML());
});

The difference is the "()" after innerHTML. This behavior seems syntactically inconsistent between how you read a value from a textbox and how you read it from a div. I'm ok with sometimes, depending on the type of control, having to read .val vs .innerHTML vs.whateverElseDependingOnTheTypeOfControl...but this example leads me to believe I now must also memorize whether I need trailing brackets or not on each property/method.

So for a person like me who is relatively new to jQuery/Javascript....I seem to have figured out this particular anomaly, in this instance, but is there a convention I am missing out on, or does a person have to literally have to memorize whether each method does or does not need brackets?

+2  A: 

innerHTML is javascript, and is a property of an element. If you'd like to stick with the jQuery version of doing things, use html():

$('#theTable .DateColumn').each(function() {
  var rowDate = Date.parse($(this).html() );
});

edit: a bit more clarification about your concerns. jQuery is pretty consistent in it's syntax. Basically, most of the methods you find allow read/write access by adjusting the parameters passed to the method.

var css = $('#element').css('color'); // read the color of the element
$('#element').css('color', 'red'); // set the color to "red"

var contents = $('#element').html(); // grab the innerHTML of the element
$('#element').html('Hello World'); // set the innerHTML of this element
Owen
Ok, so just to clarify, in my example, I have crossed over into the native javascript library??? (whose methods / properties are also exposed through the jQuery API)...so what I have to memorize is which jQuery methods / properties to call, and which corresponding native javascript ones to ignore.. Is this basically the correct idea?
tbone
Yep, although don't discount using raw javascript in certain circumstances for performance benefits. jQuery sits on top of javascript so you never actually "leave" javascript. Like any language, just use it a bit more and you'll find knowing which is which comes pretty quickly.
Owen
But, in this case, I've "left" jQuery? I'm just trying to find some consistency...I come from the VB world, so traditionally we don't, as of a matter of principle, have to worry about these case sensitivity or ninja issues. So for people like us, having to memorize these impressive edge cases is something new. But, we shall be dragged there kicking and screaming! :)
tbone
I guess we never definitevily resolved...my problem is that I, through the same object, am dealing with BOTH native javascript and jQuery....so I should try to memorize and deprectae the native Javascript functions. Correct?
tbone
Well, learn jQuery first definitely, to become familiar with it. Then, dip back into javascript when needed. jQuery can't solve everything :). But do note, in your example $(this)[0] references a javascript object. $(this) references a jQuery object. Hope that clarifies a bit more!
Owen
+1  A: 

.innerHTML is a property of the element not a method.

Property reference Example: object.MyProperty

Method Example: object.SomeFunction();

used2could
Agreed....but...are there inconsistencies in implementations that a person must memorize I guess is my question??For example, in jQuery, val(), if implemented as a property could be written and read with the same syntax. But to WRITE val(), one must say: $('#ResolvedDate').val(resolvedInputVal); which is fine I suppose, but is it consistently implemented, or with other properties do I sometimes have to say $('#ResolvedDate').val = resolvedInputVal;If it **was** inconsistent, it woud **not** be in the documentation, because the author would not know what he's doing wrong. But its Resig
tbone
...who obviously knows what he's doing, so it is safe to say it is a misunderstanding on my part...but what is the misunderstanding exactly, is my question?
tbone
Why don't you just spend some time looking over the jQuery API documentation?
Pointy
JQuery documentation states that val() is a method. i don't think your example $('#ResolvedDate').val = resolvedInputVal; is a valid syntax. only $('#ResolvedDate').val(resolvedInputVal) would work. JQuery documentation: http://api.jquery.com/val/
used2could