Hi,

I have a small script that I wrote in jQuery and I would like to convert it to vanilla javascript so I can ditch jQuery for pages that use this script.

jQuery(document).ready(function($) {

  $(document).on('click', '.wpstp_button_follow', function(e) {
    e.preventDefault();

    $.ajax({
      type: 'POST',
      url: ajaxurl+"?action=wpstp_follow",
      data: $('#wpstp_form').serialize(),
      dataType: "text",
      success: function(response) {
        var count = Number($(".follow_count").text());
        var newCount = count + 1;
        console.log(newCount);
        $('.follow_count').text(newCount);
        $(".follow_text").text("Unfollow");
        $("#follow_button").removeClass("wpstp_button_follow");
        $("#follow_button").addClass("wpstp_button_unfollow");
      }
    });

  });

  $(document).on('click', '.wpstp_button_unfollow', function(e) {
    e.preventDefault();

    $.ajax({
      type: 'POST',
      url: ajaxurl+"?action=wpstp_unfollow",
      data: $('#wpstp_form').serialize(),
      dataType: "text",
      success: function(response) {
        var count = Number($(".follow_count").text());
        var newCount = count - 1;
        console.log(newCount);
        $('.follow_count').text(newCount);
        $(".follow_text").text("Follow");
        $("#follow_button").removeClass("wpstp_button_unfollow");
        $("#follow_button").addClass("wpstp_button_follow");
      }
    });

  });

});

How would this be converted to use vanilla js?

I have also been in desperate hope of some magic converter tool that translated all of my jQuery into vanilla JS. Unfortunately, I have yet to find one that works and doesn't break the code.

However, I've been very slowly converting, little by little, by following various articles online such as:

commented: Thanks, I will check those out. +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.