Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6340979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:56:22+00:00 2026-05-24T19:56:22+00:00

I have following jQuery code $(‘a.editpo, a.resetpass’).click(function(event){ event.preventDefault(); var urlToCall = $(this).attr(‘href’); var hyperlinkid

  • 0

I have following jQuery code

$('a.editpo, a.resetpass').click(function(event){
    event.preventDefault();
    var urlToCall = $(this).attr('href');
    var hyperlinkid = '#'+$(this).attr('id');
    var targetId = $(this).attr('id').match(/\d+/);
    var targetTrDiv = '#poformloadertr_'+targetId;
    var targetTdDiv = '#poformloadertd_'+targetId;
    var currentLink = $(this).html();
    /*Todo: refactor or shorten the following statement*/
    if((currentLink=='Edit' && $('#resetuserpassform_'+targetId).is(':visible')) 
        ||
       (currentLink=='Reset Pass' && $('#account-home-container-'+targetId).is(':visible'))
        ||
       ($(targetTdDiv).html() =='')
    ){
        $.ajax({
            url:urlToCall,
            success: function(html){
                $(targetTdDiv).html(html);
                $(targetTrDiv).show();
            }
        });
    }else{
        $(targetTrDiv).hide();
        $(targetTdDiv).html('');
    }
});

The editpo and resetpass are classes applied on hyperlinks in column of table, namely Edit and Reset Pass, clicking these load up the form in a table row, ids for the respective tr and td is targetTrDiv and targetTdDiv. I am not good at JS and specially jQuery, but would still be interested in any optimisations.

But I am especially interested in reducing the condition statement.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T19:56:24+00:00Added an answer on May 24, 2026 at 7:56 pm

    First, you can optimize the following code:

       var urlToCall = $(this).attr('href');
       var hyperlinkid = '#'+$(this).attr('id');
       var targetId = $(this).attr('id').match(/\d+/);
       var targetTrDiv = '#poformloadertr_'+targetId;
       var targetTdDiv = '#poformloadertd_'+targetId;
       var currentLink = $(this).html();
    

    to:

       var wrappedSet$ = $(this);
    
       var urlToCall = wrappedSet$.attr('href');
       var hyperlinkid = '#'+wrappedSet$.attr('id');
       var targetId = wrappedSet$.attr('id').match(/\d+/);
       var targetTrDiv = '#poformloadertr_'+targetId;
       var targetTdDiv = '#poformloadertd_'+targetId;
       var currentLink = wrappedSet$.html();
    

    EDIT: Also, you could remove the currentLink=='Edit' && and currentLink=='Reset Pass' && code snippets, because you can be sure that the right links were clicked using the class selector that you are using in your jQuery click handler (a.editpo, a.resetpass).

    If you do that the codition statement will stays like this:

    if(($('#resetuserpassform_'+targetId).is(':visible')) 
            ||
           ($('#account-home-container-'+targetId).is(':visible'))
            ||
           ($(targetTdDiv).html() =='')
        ){
           /* AJAX call goes here */
        }
    

    Besides, and hopefully I’m wrong, it’s very unlikely that the condition statement can be more optimized. The reason for concluding this is that you want to much specificity that is likely impossible to achieve in other way.

    Hope it helps you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following jQuery code: $('.button').click(function(e) { e.preventDefault(); var id = $(this).attr('id'); var
I have the following jQuery code: $(.save_button).click(function() { var $form = $(this).closest(div.info_section).find(form); url =
I have the following jquery code: jQuery(function(){ jQuery(select#rooms).change(function(){ var options = ''; jQuery.getJSON(/admin/selection.php,{id: jQuery(this).val(),
I have the following jQuery code: $(ul.menu li a).click(function() { $(ul.menu li a).removeClass(currentMenu); $(this).addClass(currentMenu);
I have the following jquery code. var destTable = $(#numbers); $(document).ready(function() { $(#btnAdd).click(function() {
I have the following jQuery code: $('a[rel=close]').click(function() { alert('Close click!'); $('div#purchasePanel').hide(); }); This is
I have the following jQuery code: $('.save').submit(function(e){ var formElement = $(this); var data =
I have the following jquery code: $(function() { $('.sliding-buttons').click(slidingContent); }); function slidingContent(e) { var
I have problems with the following bit of javascript/jquery code: this.droppable = function(){ $('.imageWindow
I have the following jQuery code $(#dropdown).hover(function() { $(this).stop(true,true).fadeTo('fast',1); $(#options).stop(true,true).slideDown(); }, function() { $(this).delay(1000).stop(true,true).fadeTo('fast',0.1);

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.