Requesting member of node_element results in “undefined”

I'm using Opa for a school project in which there has to be some synchronization of a textfield between several users. The easy way to solve this, is to transmit the complete field whenever there is a change performed by one of the users. The better way is of course to only transmit the changes.

My idea was to use the caret position in the textfield. As a user types, one can get the last typed character based on the caret position (simply the character before the caret). A DOM element has an easy-to-use field for this called selectionStart. I have this small Javascript for this:


which correctly returns 5 if the caret stands at the fifth character in the field. In Opa, I cannot use selectionStart on either a DOM or a dom_element so I thought I'd write a small plugin. The result is this:

##extern-type dom_element

##register jsGetCaretPosition: dom_element -> int
    return node.selectionStart;

This compiles with the opp-builder without any problem and when I put this small line of code in my Opa script:

#pos = %%caret.jsGetCaretPosition%%(Dom.of_selection(Dom.select_id("content")));

that also compiles without problems. However, when I run the script, it always returns "undefined" and I have no idea what I'm doing wrong. I've looked in the API and Dom.of_selection(Dom.select_id("content")) looked like the correct way to get the corresponding dom_element typed data to give to the plugin. The fact that the plugin returns "undefined" seems to suggest that the selected element does not know the member "selectionStart" eventhough my testcode in Javascript suggest otherwise. Anyone can help?


In Opa dom_element are the results of jQuery selection (i.e. an array of dom nodes). So if I well understood your program you should write something like node[0].selectionStart instead of node.selectionStart.

Moreover you should take care of empty selection and selection which doesn't contains textarea node (without selectionStart property). Perhaps the right code is tmp == undefined ? -1 : tmp = node[0].selectionStart == undefined ? -1 : tmp

Need Your Help

Declaring a table of type object where the object contains two nested tables

sql oracle plsql oracle-apex

I am creating staff object who can teach on 1 or many modules. I have a nested table that contains refs to the module table. I was also going to create another to show the modules that the staff me...

How do I use MVC jQuery and Ajax together in order to return Json objects

c# razor

I want to be able to click a button, have a function in my controller return a JSON object that can be used on my page without the page reloading.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.