Showing posts with label Reference. Show all posts
Showing posts with label Reference. Show all posts

Get Selected Item in JavaScript Form

Somehow JavaScript mixed objects with attributes. For instance:

document.formName.listBoxName[i].selected

formName and listBoxName are references to two bojects. It might be good, but it just crosses the boundary of reference to object and the attributes. Don't know if this the the reason, to get selected item's value in form seems not easy. Google search shows there are so many people ask the question and not get right answers. Here is the easier way:

; function getValueOfSelectedItem(obj)
{ ; var val = obj.options[obj.selectedIndex].value
; if (val) (use val here)
}

To call this function, put the caller on onchange in HTML codes:

onChange="getValueOfSelectedItem(this)"

The beauty of it is there is no mixture of reference and attribute and you can pass anything of it without worry of crossing boundary issue.


http://www.java2s.com/Code/JavaScriptReference/Javascript-Properties/selectedIndexExample.htm

Issues In Multidimensional Array in JavaScript

Multidimensional array in JavaScript is treated as object, so it would be a problem when it works with dynamic HTML and Ajax. Since the reference points to the multidimensional array needs to be closely working with string, it is hard to program when should such reference being instantiated, that is presenting the whole body of the array rather than the reference itself. This issue becomes very serious when parameter management gets involved. Indeed, both bind variable and parameter management are essential for dynamic HTML.

If a string used as reference to be passed between functions, another issue may be occurred: hard to convert it back to reference. In some cases eval() function can be used to achieve this goal, however, it won't be able to work with multidimensional array.

The solution is using JSON datatype to represent the multidimensional array. In addition, it is suggested to convert it into string before being composed into bind variable or get involved with parameter management.


http://koncordpartners.blogspot.com/2010/08/parameter-management.html

Labels