-3

I have a pure JS function in which i have tried to convert majority to jquery. How do I set the entire function to a proper jquery syntax?

 function openResources(e, resourceName) {
    var i, tabcontent, tabs;
    tabcontent = $(".resources__all-tabs-container");

    for (i = 0; i < tabcontent.length; i++) {;
        tabcontent[i].style.display = "none";
    }

    tabs = $(".resources__tabs");

    for (i = 0; i < tabcontent.length; i++) {
        tabs[i].className = tabs[i].className.replace(" 
 resources__all-tabs", "");
    }

    document.getElementById(resourceName).style.display= "block";
    //$(`"`+ resourceName + `"`)[0].style.display ="block";
    e.currentTarget.className += " resources__all-tabs";

 }
Aditya Pattani
  • 75
  • 2
  • 14

5 Answers5

1

This:

tabcontent = $(".resources__all-tabs-container");
for (i = 0; i < tabcontent.length; i++) {;
    tabcontent[i].style.display = "none";
}

Would be:

$(".resources__all-tabs-container").hide();

see here: http://api.jquery.com/hide/

This:

tabs = $(".resources__tabs");
for (i = 0; i < tabcontent.length; i++) {
    tabs[i].className = tabs[i].className.replace("resources__all-tabs", "");
}

Should be replaced with:

$(".resources__tabs").removeClass("resources__all-tabs")

See here: https://api.jquery.com/removeclass/

This:

document.getElementById(resourceName).style.display= "block";

Has to be replaced with:

$("#" + resourceName).show();

See here: http://api.jquery.com/show/

This:

e.currentTarget.className += " resources__all-tabs";

Should be:

$(e.currentTarget).addClass("resources__all-tabs");

See here: jquery addClass() not working with event.target

D. Braun
  • 508
  • 1
  • 4
  • 11
0

Don't forget you can chain functions in jQuery.

function openResources(e, resourceName) {
  $(".resources__all-tabs-container").hide();
  $(".resources__tabs").removeClass('resources__all-tabs');
  $('#' + resourceName).show();
  $(e.currentTarget).addClass('resources__all-tabs');
}
Zenoo
  • 12,670
  • 4
  • 45
  • 69
0
function openResources(e, resourceName) {
  $(".resources__all-tabs-container").hide();
  $(".resources__tabs").removeClass("resources__all-tabs");
  $("#" + resourceName).show();
  $(e.currentTarget).addClass("resources__all-tabs");
}

Should be the equivalent code.

void
  • 36,090
  • 8
  • 62
  • 107
0
function openResources(e, resourceName) {
    var tabcontent = $(".resources__all-tabs-container"), 
        tabs = $(".resources__tabs");

    // hide all tabs and show the one that matches `resourceName`
    tabcontent.hide().filter('#' + resourceName).show();

    // set current tab as selected
    $(e.target).addClass('resources__all-tabs')
}
mplungjan
  • 169,008
  • 28
  • 173
  • 236
Slim
  • 1,924
  • 1
  • 11
  • 20
-1

Something like this:

function openResources(e, resourceName) {
      $('.resources__all-tabs-container').each(function() {
          $(this).hide();
      })

      $('.resources__tabs').each(function() {
          $(this).removeClass('.resources__tabs');
      })

      $('#'+resourceName).show();
      $(e.target).addClass('resources__all-tabs');
   }
Tony Stonks
  • 360
  • 3
  • 13