[ 3 posts ]

mc_hendriks

  • Joined: Sat Apr 24, 2010 5:53 am
  • Posts: 43
  • Offline
  • Profile

Trying to get a value

Post Posted: Sat Sep 18, 2010 3:47 am
+0-
I'm trying to create 2 simple functions to set and to get a value from an input field on a form. These functions should later be used from another form and vice versa.

Setting the value works ok like expected.
Getting the value gives me an undefined value after returning the value.

Any suggestions how to solve this or perhaps a clue to achieve the same functionality.

Thanks in advance

URL to the example:

http://www.digirent.nl/psmyui/vb/getsetvalues.html

Code:
function set_value(id,val) {
  AUI('node').ready(function(A) { A.get('#'+id).set('value',val); })
}
function get_value(id) {
var value;
  AUI('node').ready(function(A) {
    value=A.get('#'+id).get('value');
    alert('1 value:'+value);
 });
    alert('2 value:'+value);
}

AUI('node').ready(function(A) {
    set_value('klant','Hendriks');
    var retval=get_value('klant');
    alert('3 retval:'+retval);
});

Eduardo Lundgren

YUI Contributor

  • YUI Developer
  • Offline
  • Profile

Re: Trying to get a value

Post Posted: Sat Sep 18, 2010 10:11 am
+0-
Hi, you can use the .val() method as getter and .val('new value') as setter. See the documentation here on aui-node page: http://alloy.liferay.com/deploy/api/A.N ... method_val

var node = A.one('#input');

node.val('testing'); // setting the value

alert( node.val() ); // output "testing"

Nate Cavanaugh

YUI Contributor

  • Offline
  • Profile

Re: Trying to get a value

Post Posted: Sun Sep 19, 2010 12:15 pm
+0-
I also think the format of the AUI() code might cause some problems.

Try this instead:

AUI().ready('aui-node', function(A){
A.one('#'+id).val();
})

But part of this might be the format, and the fact that inside of your function, you're doing an alert outside of the ready.

AUI().ready(function(){}) is asynchronous, so your alert('2 value') might fire before the alert('1 value') because the "ready" event might not have happened yet, or it might be taking a longer time to pull down the package.

This is one of the issues with returning data and asynchronous functions.

Since .val() will act as a getter and setter, is it possible to use that inside of your app rather than the global get_value function?
  [ 3 posts ]
Display posts from previous:  Sort by  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum