简体   繁体   中英

How to only invoke a function that's called from within another function when a specific div is NOT clicked?

I am calling campusInfo(id) a few places in my code. Inside campusInfo(id) function I am calling campusInfo_2(HouseID) .

One of the instances where I am calling campusInfo(id) is on the onclick of a div. In this case I don't want campusInfo_2(HouseID) invoked.

Here is the campusInfo(id) javascript function which calls campusInfo_2(HouseID)

//campus information
function campusInfo(id) {
    fetch(`https://api/` + id)
        .then(r => r.json())
        .then(r => {
            const mapClassToResponse = {
                '.campus-name': 'name',
               '.program-levels-values:eq(4)': 'annualTuition'
            };
            var HouseID = r['Houses'][0]['HouseID'];

        Object.keys(mapClassToResponse).map(k => {

            $(k).html(r[mapClassToResponse[k]]);
        });

        //formatting numbers to have commas
        $(".program-levels-values:eq(4)").digits();
        $(".campus-number:eq(0)").digits();
        $(".campus-number:eq(1)").digits();            

        //program-levels
        $(r.programLevels).each(function (index, item) {
            $('.program-levels-values:eq(0)').append(item.programLevel + ' ');
        });

        //institution control
        if (r.isInstitutionControlPublic == false) {
            $('.program-levels-values:eq(2)').html('Private');
        } else {
            $('.program-levels-values:eq(2)').html('Public');
        }

campusInfo_2(HouseID);            
    }).catch(console.log);
 };

Here is the div where I don't want campusInfo_2(HouseID) being invoked

  <div class="other-school" onclick="campusInfo(10)"><label 
  class="other-schools-list">Portland State University</label</div>

Here is what I tried thus far but it was saying in console that the element was undefined.

  if(document.getElementsByClassName('other-school').clicked == false)
        {
        campusInfo_2(HouseID);
        }

One suggested solution would be to provide an additional argument.

<div class="other-school" onclick="campusInfo(10, false)">

Then your method definition would change to:

function campusInfo(id, skipSecondCampusInfo) {

If you only pass that in on the inline binding, then it will be undefined on all the other calls. So then your call could be surrounded with:

if (skipSecondCampusInfo !== false) campusInfo_2(HouseID);

In which case if it is undefined, it will execute it. Otherwise, if it is false, it will not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM