Krystyna Krystyna - 6 months ago 25
Javascript Question

Modify jquery to server-side for link true/false toggleClass

This code is in pseudo jquery, but how do I make it as jquery code?

I need this to toggle an icon on or off, depending on its selection from an admin page, then display this selection on other pages as a static badge status.

So these two snippets, can they be modified to include the toggle action from the second snippet into the first snippet?

I need it to work with C#, since the backend will be written in C#.

I am scouring the net to modify code like this, because I dont know jquery myself, I need to get help with it... :/



//first snippet
$('a').click(function(evt) {
evt.preventDefault();
var link = $(this).attr('href');
$.ajax({
method: post,
url: '/link-tracking',
data: {
href: link
}
});
}).done(function(url) { // pass the url back to the client after you incremented it
window.location = url;
});

//second snippet
$('.verify').click(function() {
var id = $(this).parents('div').attr('id');
$(this).toggleClass('verified');
$.post('/page.html', {
'verified': $(this).hasClass('verified'),
'id': id
},
function(data) {
if (data.verified) {
$(this).toggleClass('verified');
}
//some sort of update function here
});
});




Answer

Only single click handler appear necessary to meet requirement

$('.verify').click(function() {
  var id = $(this).parents('div').attr('id');
  $(this).toggleClass('verified');
  $.post('/echo/json/', {json:JSON.stringify({
      'verified': $(this).hasClass('verified'),
      'id': id
      })
    },
    function(data) {
    console.log(data)
      if (data.verified) {
        // console.log(data)
      }
      //some sort of update function here
    });
});

jsfiddle https://jsfiddle.net/516rx7xa/1/