You are viewing limited content. For full access, please sign in.

Question

Question

JS executing multiple times on table row

asked on March 14, 2024

This code that runs on 3 tables Master Permits, Permits, and Sub Permits.  It is executing multiple times per table row on the Master and Sub tables. Resulting in duplicate <p>'s.  The Permits table only executes once as expected. All 3 tables have the class permit, one field with the class permitNumber, and one field with the class formsResLink. The sub permit fields are currently shown to verify that only one distinct row is retrieved from the lookup.

 

$(document).ready(function () {
    //Set Create Inspection href
    $('.createInspectionLink a').prop('href', "https://Somewherelf.com/Forms/DEVInspections?Submitted_By=" + $('.initiator input').val() + "&Permit_Number=" + $('.startingPermit input').val() + "&Location=" + $('.location input').val() + "&Permit_EntryID=" + $('.entryID input').val() + "&Permit_Id=" + $('.thisPermitId input').val() + "&Document_Type=Permit");
	
    $('.permit').on("change", ".permitNumber input", function () {
        createPermitLink.call(this);
    });
    $('.permit').on("change", ".formsResLink input", function () {
        createPermitLink.call(this);
    });

});
function createPermitLink(inputField) {
    var tableRow = $(this).closest('tr')
    var permitNumber = tableRow.find('.permitNumber input').val();
    var resLink = tableRow.find('.formsResLink input').val();
    var workClass = tableRow.find('.workClass input').val();
    var thisVal = $(this).val();
    if ((resLink == '') && (thisVal !== resLink)) {
      	console.log(tableRow.find('.permitlink'));
        tableRow.find('a').remove();
      	tableRow.find('p').remove();     	
        tableRow.find('.formsResLink input').after($('<p class="permitlink">' + permitNumber + ' - ' + workClass + ' (Finaled)</p>'));
        //alert(permitNumber + ' - ' + workClass + ' (Finaled)</p>');
        tableRow.find('.formsResLink input').hide();
    }
    if ((resLink !== '') && (thisVal !== permitNumber)) {
        tableRow.find('a').remove();
        tableRow.find('p').remove();
      	tableRow.find('.permitlink').remove();
        $(this).after($('<a class="permitlink" href="' + resLink + '" target="_blank">' + permitNumber + ' - ' + workClass + '</a>'));
        //alert(resLink + '" target="_blank">' + permitNumber + ' - ' + workClass + '</a>');
        $(this).hide();
    }
    //Hide Table Row delete X's
    $('.permit td.action').hide();
    $('.permit td.col0, .cf-col-label').hide();
    //Hide Table Row add links
    $('.permit .cf-table-add-row').hide();
}

 

0 0

Answer

APPROVED ANSWER SELECTED ANSWER
replied on March 14, 2024

The Master Permit and Sub Permit tables have an additional input of type "hidden" for some reason.  I updated my code to specify '.formsResLink input[type=text]' to prevent this from running on the hidden input. This prevents the duplicate items from being created.

0 0

Replies

You are not allowed to reply in this post.
You are not allowed to follow up in this post.

Sign in to reply to this post.