I’ve got a page which loads an external js and on the page I’ve got a function called calculate_totals(). What I want is to call the function calculate_totals() from a function in the external js.
This is a piece of JavaScript which loads in an external js
function selectItemMouse(thisVar)
{
var searchValue = thisVar.attr('data-name');
thisVar.parent().parent().parent().parent().children('.list').val(searchValue);
//alert(thisVar.parent().parent().parent().parent().next().find('input').focus());
thisVar.children().children().each(function()
{
var dataId = $(this).attr('data-id');
var value = $(this).attr('data-name');
//This change the input of my form
$(thisVar).parent().parent().parent().parent().children().children('.'+dataId).val(value);
});
$('.customerResults').hide();
calculate_totals();
}
Based on commenting session:
calculate_totals is defined inside of
$(document).ready(function(){});Because of that is not seen outside it. Basically, it is visible inside
$(document).ready(function(){});only. Just move it outside it. Now it will became global and visible anywhere.Instead of
use:
After that you should have it available inside selectItemMouse and any other place.
Besides, I would think on updating your code.
usage of such chains is not flexible. Just you will wrap thisVar element with some other element – you will need to update your JS code.
Take a look at
.parentsor.closest. For instance, you may change a line above like this:Where
.class_common_parentis a class of an element which you take usingparent().parent().parent().parent().