Page 1 of 1
val() returning null
Posted: 2019-09-30 06:20
by Leving Tinoco
Hi, I have some problem with some code on my hook files. I have this code to
update a fields amount
var update_value2 = function(){
var vvalue1 = $j('#value1').val();
var idcategory = $j('#category_id-container').val();
the error happend here, in the lookup fields
console.log(vvalue1);
console.log(idcategory);
I need the value from idcategory but every time the returns value from
$j('#category_id-container').val(); is null. until I update with another
value it's fine, just append after I open the windows for the first time.
it is somebody else having the same problem?
thanks.
Re: val() returning null
Posted: 2019-10-01 12:06
by onoehring
Hi Leving,
If I understand your coude correct, this happens, always, but only in new records. - Correct?
If so, this is the normal behavior, as new records do not have an ID.
Lookup fields tend to have another problem which occurs in several places in AG generated applications: AG makes heavy use of ajax and populates lookup after the page is loaded. For this reason the ID might not be available at page-load time when your JS might be run.
I had a similar problem (question here:
viewtopic.php?f=2&t=3102&p=10355&hilit= ... ext#p10355 , solution here:
viewtopic.php?f=2&t=3104&p=10381#p10381 ).
Short: My solution is a function that continually checks and does something once.
Olaf
Re: val() returning null
Posted: 2019-10-02 06:26
by Leving Tinoco
Happen went I open the windows for edit it is a lookup field and I need the Id value using the code:
function update_value2(){
var vvalue1 = $j('#value1').val();
if(isNaN(vvalue1)){
return show_error('value1', 'The value should be number');
}
var idcategory = $j('#category_id-container').val(); << - Here
console.log(vvalue1);
console.log(idcategory);
$j.ajax({
url: 'hooks/ajax-category-mult.php?id=' + idcategory,
type: 'get',
cache: false,
success: function (response) {
totalvalue = response * vvalue1;
$j('#value2').val(totalvalue.toFixed(2));
}
});
};
$j('#category_id-container, #value1').change(function(){update_value2();});
I need the value from id number from category_id-container (lookup field), every time I modify value1 (real value) $j('#category_id-container').val() return null. ultil I change to another item on the list the value return is correct.
Re: val() returning null
Posted: 2019-10-02 07:01
by onoehring
Hi,
you want the ID from the referenced table (lookup) and not from the record shown on screen?

- ec101.png (34.38 KiB) Viewed 4819 times
I used this to get the JS path for the field:

- ec_getjspatha.png (11.24 KiB) Viewed 4816 times
This is the JS path that works in my example to get the value from the field
In my case, this selector path returns the correct value each time I a) open a record b) change the lookup to something directly, so, I do not need to save the record. This value changes immediately when I select something else.
So, at this time I see no reason, why a similar selector should return an empty value if something is selected.
Oh, in the excellent Udemy course Ahmed prepared, is a session "Section 11 - Lesson 1127 - Performing calculations in the detail view" and "Section 11 - Lesson 1128 - Calculating subtotal from unit price, discount and quantity" which show how to do it in more detail.
Olaf
Re: val() returning null
Posted: 2019-10-02 08:00
by pbottcher
Hi,
just to clarify,
I need the value from idcategory
what do you need? the actual value, or the lookup value (=reference to the actual value)?
Re: val() returning null
Posted: 2019-10-06 19:54
by Leving Tinoco
the actual value, not the lookup list value.
Re: val() returning null
Posted: 2019-10-07 07:00
by pbottcher
Hi,
try
Code: Select all
var idcategory = $j('#s2id_category_id-container').find('.select2-chosen').text();