val() returning null

Please report bugs and any annoyances here. Kindly include all possible details: steps to reproduce, expected result, actual result, screenshots, ... etc.
Post Reply
Leving Tinoco
Posts: 12
Joined: 2018-01-04 18:39
Location: Canada

val() returning null

Post by Leving Tinoco » 2019-09-30 06:20

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.

User avatar
onoehring
AppGini Super Hero
AppGini Super Hero
Posts: 1156
Joined: 2019-05-21 22:42
Location: Germany
Contact:

Re: val() returning null

Post by onoehring » 2019-10-01 12:06

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

Leving Tinoco
Posts: 12
Joined: 2018-01-04 18:39
Location: Canada

Re: val() returning null

Post by Leving Tinoco » 2019-10-02 06:26

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.
Attachments
2019-Selection_007.png
2019-Selection_007.png (15.64 KiB) Viewed 3479 times
2019-Selection_005.png
2019-Selection_005.png (14.12 KiB) Viewed 3479 times
2019-Selection_004.png
2019-Selection_004.png (5.25 KiB) Viewed 3479 times
2019-Selection_003.png
2019-Selection_003.png (4.12 KiB) Viewed 3479 times

User avatar
onoehring
AppGini Super Hero
AppGini Super Hero
Posts: 1156
Joined: 2019-05-21 22:42
Location: Germany
Contact:

Re: val() returning null

Post by onoehring » 2019-10-02 07:01

Hi,

you want the ID from the referenced table (lookup) and not from the record shown on screen?
ec101.png
ec101.png (34.38 KiB) Viewed 3475 times
I used this to get the JS path for the field:
ec_getjspatha.png
ec_getjspatha.png (11.24 KiB) Viewed 3472 times
This is the JS path that works in my example to get the value from the field

Code: Select all

$j("#ID_Location").val()
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

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1635
Joined: 2018-04-01 10:12

Re: val() returning null

Post by pbottcher » 2019-10-02 08:00

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)?
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

Leving Tinoco
Posts: 12
Joined: 2018-01-04 18:39
Location: Canada

Re: val() returning null

Post by Leving Tinoco » 2019-10-06 19:54

the actual value, not the lookup list value.

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1635
Joined: 2018-04-01 10:12

Re: val() returning null

Post by pbottcher » 2019-10-07 07:00

Hi,

try

Code: Select all

var idcategory = $j('#s2id_category_id-container').find('.select2-chosen').text();
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

Post Reply