Jalon Juanis Jalon Juanis - 5 days ago 5
CSS Question

Jquery click function not working when clicked

I'm facing problem to trigger jquery click function. I want to hide phone number and email using jquery. The phone number and email only can see if using click the

<p>
tag.

Below is my jquery function that I already write;

function() {
var $phone = '123456123',
$email = 'info@youremail.com',
phone = $('#phone'),
email = $('#email');

$(phone).click(function() {
var text = $(this).text() == $phone ?
'Click to view number' : $phone;
$(this).text(text).toggleClass("active");
});

$(email).click(function() {
var text = $(this).text() == $email ?
'Click to view email' : $email;
$(this).text(text).toggleClass("active");
});

}();


When I run the code, I click on the number tag but nothing change.

Answer

Just needed a small fix on js. See it working here

$(function() {
    var $phone = '123456123',
          $email = 'info@youremail.com',
            phone = $('#phone'),
              email = $('#email');

    $(phone).click(function() {
        var text = $(this).text() == $phone ?
            'Click to view number' : $phone;
        $(this).text(text).toggleClass("active");
    });

    $(email).click(function() {
        var text = $(this).text() == $email ? 
            'Click to view email' : $email;
        $(this).text(text).toggleClass("active");
    });
});