1

I'm using jQuery hide on click for my ads, so I have this code:

$(document).ready(function() {
$(".myadcss").click(function() {
$(this).hide(1000) 
    });

now I want to disable this function if some one clicks out from ads, so if somone click on transparent color?

enter image description here

Demo Ad: 728x90

Demo Full Ad:

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Lani
  • 73
  • 8
  • It's a bit unclear what you want enabled and what you want disabled, and when. Try and clarify. Also, the opposite of `hide` is `show` if that is your question. – kabanus Jun 03 '18 at 10:05
  • so some banner ads have a transparent space, and if somone click in this trasparent space, this function hide my ads without being clicked, i want to hide the ads only after being clicked, sorry my English is nto that good, but thanks for your help – Lani Jun 03 '18 at 10:12
  • @Lani can you show some of your code – לבני מלכה Jun 03 '18 at 10:17
  • Possible duplicate of [Use jQuery to hide a DIV when the user clicks outside of it](https://stackoverflow.com/questions/1403615/use-jquery-to-hide-a-div-when-the-user-clicks-outside-of-it) – Mohammed Joraid Jun 03 '18 at 10:22
  • Thanks for your help i have see that post before asking this question – Lani Jun 03 '18 at 10:29
  • Thanks all for yours help, i have edit my question again, pleas Take a look. Thanks – Lani Jun 03 '18 at 10:46
  • i have add a demo link so like this anyone understand that what i want to do, thanks again – Lani Jun 03 '18 at 11:20
  • **"this function hide my ads without being clicked, i want to hide the ads only after being clicked"** We do not know what is your ads CSS or class name or what HTML element it is. If your problem is that the AD gets hide() without being clicked, then you are selecting the wrong element and calling the wrong function. So what is $(".myadcss")?? – Mohammed Joraid Jun 03 '18 at 11:27
  • Thanks, so I made some changes on this plugin so the class name now is `.adclick-pro-front` Thanks again – Lani Jun 03 '18 at 11:32

3 Answers3

1

first, your code example is not complete and might throw an error, as the first function is not closed:

$(document).ready(function() {
  $(".myadcss").click(function() {
    $(this).hide(1000)
  });
});

Now, only add the class "myadcss" to your ad banners. With that, the click function will only be triggered on your ad banners and not somewhere else.

Additionally, if you want to prevent hiding your banner, if some specific element was clicked, you can check the clicked elements class name and decide to abort your function in that case:

$(document).ready(function() {
  $(".myadcss").click(function(event) {
    if(event.target.classList.contains('specific')) {
      return;
    }
    
    $(this).hide(1000)
  });
});
.myadcss {
  height: 200px;
  width: 50px;
  background: red;
}

.specific {
  margin: 30px 5px;
  height: 30px;
  background: white;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>example</title>
</head>
<body>

  <div class="myadcss">
    Your banner text
    <div class="specific"></div>
  </div>
</body>
</html>

Check clicking the white area: Above JavaScript will check its class name and will abort executing further code by using the return statement.

MarvHock
  • 793
  • 7
  • 16
0

Instead of disabling this function, add an if condition inside the function so that it hides the ads only if someone clicks the banner ads and returns otherwise.

Edit: From the screenshots that you pasted, it looks like you want to hide the ad if someone clicks on only the ad. If you added the myadcss class only to your ad element, your function will not be triggered if you click anywhere outside the ad element.

Madhan Kumar
  • 121
  • 7
  • Thanks for your help, my question now is how can i do this? Thanks – Lani Jun 03 '18 at 10:13
  • I am assuming you need myadcss class to set some css styles for your ad element. I would suggest to add another class to your banner ad and add your click event to that class. – Madhan Kumar Jun 03 '18 at 10:21
  • thanks for your help, i have edit my question again, pleas Take a look. Thanks – Lani Jun 03 '18 at 10:45
  • can you post some screenshots? I don't understand what you meant by transparent color. – Madhan Kumar Jun 03 '18 at 10:47
  • Thanks again, I have add a screenshot – Lani Jun 03 '18 at 10:53
  • that transparent color is same body with ads, i can chage it to make red example, but it is one body with the ads, if somone click in that part, that just colse the ads, for that i want to disable the click in that part – Lani Jun 03 '18 at 11:07
  • remove the class from the body and add it the ad elements then – Madhan Kumar Jun 03 '18 at 11:13
  • i have add a demo link so like this anyone understand that what i want to do, thanks again – Lani Jun 03 '18 at 11:20
  • Really i don't know that, but this is a WordPress Plugin – Lani Jun 03 '18 at 11:25
0

From #this_answer

if (!container.is(e.target) && container.has(e.target).length === 0)

To summurize, first, you use your code to initiate hide when your banner ads are being clicked:

$(document).ready(function() {
$(".myadcss").click(function() {
$(this).hide(1000) 
    });

Then you monitor clicking (using mouse-up event) to see if the pointer is clicking on the same element or another element. If it's another outside element, then show your banners ads.

$(document).mouseup(function(e) 
{
    var container = $(".myadcss");

    // if the target of the click isn't the container nor a descendant of the container
    if (!container.is(e.target) && container.has(e.target).length === 0) 
    {
            //Your Logic Here
            // call e.g. banners_show()
        container.show();
    }
});
Mohammed Joraid
  • 6,202
  • 2
  • 27
  • 38
  • thanks i have try this code before asking this question – Lani Jun 03 '18 at 10:30
  • @Lani then you should post everything you tried in your question and explain what didn't work. – Mohammed Joraid Jun 03 '18 at 10:31
  • thanks for your help, i have edit my question again, pleas Take a look. Thanks – Lani Jun 03 '18 at 10:45
  • i have add a demo link so like this anyone understand that what i want to do, thanks again – Lani Jun 03 '18 at 11:19
  • @Lani this is the closest of what seems you are looking for (https://jsfiddle.net/boqLw6yj/). This works. If it doesn't for you, means you have the wrong HTML or you are not using the JQuery selector probably. – Mohammed Joraid Jun 03 '18 at 11:30
  • I want the ad to be removed after being clicked, to show it again is not problem, i can do that from my WP Dashboard, all what i want to do is to be sure that add is clicked and after that to hide the add, Thanks – Lani Jun 03 '18 at 11:35
  • Did you check the JS Fiddle link in my last comment? this one>> http://jsfiddle.net/boqLw6yj/ N.P You have to click on the green square to see it working. – Mohammed Joraid Jun 03 '18 at 11:50
  • yes your code is corect, but still is not helping to me, Thanks @MJoraid – Lani Jun 03 '18 at 11:57