Auto Mouse click on a content within a div?

Asked

Viewed 2,417 times

2

I wonder if it is possible to make a div with auto click.

That would be how the page loads and when someone clicks on any part of the page the mouse will click on the content inside the div.

in case I put an iframe of an external page. but it could be an image or a video.

if possible do this with java script. and that once the click has happened the same person can not click again for 1 day. or an interval of time.

 <div id='conteudo'>  
 <iframe frameborder='0' height='260' name='iframe1' scrolling='no' src='url' width='336/>   
</div>
  • $("#content"). click(Function(){/does something/});

  • Explains better.

2 answers

3

This way it won’t work, as you will be actually clicking on the element within the iframe. Using jquery can be used the Contents method within the iframe

Would look like this:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
var iframeContents = $('iframe').contents()[0];
$(iframeContents).find('seletor').click(function () {
// Aqui entra o seu evento!
});

  • Show me an example of how it works. if possible

  • The example is on top. The click itself can be in the body element if you prefer. This would look like this: var iframeContents = $('iframe'). Contents()[0]; $(iframeContents). find('body'). click(Function () { Alert('click on body'); })

  • Can’t get it to work. shows me it working with some iframe

0

As long as you have some event tied to click in this area of yours (which I will call #meu_elemento), you can do something like that:

$('#meu_elemento').trigger('click');

You can leave the event as the default of the element, or make a bind on account, as

$('#meu_elemento').on('click', function(e){
    $('.target').append('você clicou em #meu_elemento');
});

See the working example:

$('#meu_elemento').on('click', function(e){
    $('.target').append('você clicou em #meu_elemento');
});


setTimeout((function() {
    $('#meu_elemento').trigger('click');
}), 5000);
#meu_elemento{
    width: 100px;
    height: 100px;
    background-color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="meu_elemento"></div>

<div class="target"></div>

See that I put one setTimeout() in the trigger() for you to see it in action (note that if you click on the red square, the event repeats). What it does is a simple append() a text. From there, you can treat your event from click however you see fit.

  • For example : this iframe . how would I click the play on it with this code you made? <iframe width="560" height="315" src="https://www.youtube.com/embed/OuZQNgvNw6s?controls=0&amp;showinfo=0" frameborder="0" allowfullscreen></iframe>

  • @user3192159 your goal is to play the video automatically?

  • I added an iframe in the <div id="meu_elemento"> video iframe yotube </div> but after 5 seconds a message appears " you clicked #meu_elemento" but the click on the video inside the div does not happen.

  • Your question was about auto-click inside a div, which is what I based my answer on. In my comment, I asked you if your goal is play the video automatically. If yes, things change shape... So this is your goal?

  • The video is an example of the element that would be in the div. could be a link, a pop up, an image.

  • Well, generically it gets a little more complicated. iframes are complicated. In the specific case of youtube embedded videos, autoplay depends on a parameter of the src. There’s a nice discussion about it in gringo http://stackoverflow.com/questions/6246939/start-play-embedded-iframe-youtube-video-on-click-of-an-image But for other elements, the solution is this. Remember that the click will happen in the element you set, and not in his children

  • What if it was a link ? <a href="https://www.youtube.com/watch?v=OuZQNgvNw6s"></a>

  • http://stackoverflow.com/questions/7999806/jquery-how-to-trigger-click-event-on-href-element

  • those Pop up that open automatically when we enter a website and click on some link. how does it work? wouldn’t be the same thing.

  • I think that then the scope of your question changes completely. pop ups can be done in many ways

  • I don’t want a pop up, what I’m trying to do is that any object that is in the specific div is auto clickable.

  • The code you made is part of the goal. it just does not click on the content in the iframe

  • It probably doesn’t click because it’s not on the same domain. In order to manipulate iframe it needs to be on the same domain as your site. If not, imagine doing a Facebook iframe and then do $('postagem').val('Acesse o site xxx.xxx'); $('submit').click();. Everyone who joined the site (and was logged in to facebook) would upload the iframe and post content on facebook. Imagine this on a bank site... Anyway. If your site is abc.com you can’t manipulate iframe from another site without being abc.com. ;)

Show 8 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.