I have the following code, I want a user to be able to copy or paste into a text field,
basically imitating a ctrl+c or ctrl+v
My problems are:
- how to make sure the context menu only appears in text fields
- how to paste the text into the region.
I have been through the docs and so far I have this NOT working:
var menu = Ti.UI.createMenu();
menu.addItem('Copy', function() {
var data =$.("#this").val()
Ti.UI.Clipboard.getData('text/plain',data);
});
menu.addItem('Paste', function() {
var data =Ti.UI.Clipboard.getData('text/plain');
$.("#this").val(data)
});
function showrightmenu(){ Ti.UI.getCurrentWindow().setContextMenu(menu);}
I could call this using oncontextmenu= "showrightmenu()" but now, how to paste something in this line:
$.(#this).val(Ti.UI.Clipboard.setData('text/plain'))
AM GROPING IN THE DARK. I’m a newbie to TideSDK, this is my first project.
Question 1
I’m new to TideSDK as well and might be wrong, but as far as I can tell from the API documentation, context menus are bound to a window, and displaying different context menus when right clicking different parts of the window would require you to each time change the window’s context menu…
Example:
This way, when you click the element with id
some-element, the first context menu is shown, and when you click the#text-fieldelement, the second context menu is shown.Note that this won’t work if one element is inside the other, because then both events are fired.
Question 2
In this code you supplied:
You want to use
Ti.UI.Clipboard.getDataand notTi.UI.Clipboard.setData, since you want to get the text stored in the clipboard and then put it into the text field. Also, you might want to change$.(#this)to$("#this")or$(this).This should work:
Remark
You seem to be confused about how to use jQuery. To select an element, you use
$()and not$.(). Also, with$("#example")you select the DOM element with the idexample.$(this)is used inside a function called when an event is fired, and refers to the element on which the event was fired.$("#this")is not the same as$(this). Hope that helps a bit…