0

I have a problem on my website. I have a <div id="test"> which is display:none by default.
Now, when I click on the button it changes to display:block, but in my div a popup is displayed which disappears when I make a click.
So I need to simulate a click so that the another div does not show.
I tried :

<script>
  document.getElementById('test').setAttribute('class','display-block');
  document.getElementById('test').click();
</script>

But it does not work.

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
TanGio
  • 766
  • 2
  • 12
  • 34
  • 1
    Can you give some more details about your problem? Could not able to understand it properly? What you want to achieve and what is your problem? – SK. May 29 '15 at 13:00
  • possible duplicate of [How to use simulate the action of clicking button with JQuery or Javascript](http://stackoverflow.com/questions/5015054/how-to-use-simulate-the-action-of-clicking-button-with-jquery-or-javascript) – Alex McMillan May 29 '15 at 13:02
  • if you want to use a javascript check here: [enter link description here][1] [1]: http://stackoverflow.com/questions/2705583/how-to-simulate-a-click-with-javascript – user3744810 May 29 '15 at 18:27

4 Answers4

1
// element you click to execute the display   
$('#button').on('click', function(){      
      $('#test').css('display','block');
});
lucho20pt
  • 77
  • 5
0

You can use jquery to do it:

$('#test').click()

or

$('#test').trigger("click");

Check the demo here: http://jsfiddle.net/eq9c2p2c/

update

You need to add click event, then you just trigger it.

This is the HTML:

<div id="test">click</div>

Javascript:

$('#test').click(function() {
    $(this).addClass("border")
})
$('#test').click()

http://jsfiddle.net/eq9c2p2c/2/

Radonirina Maminiaina
  • 6,958
  • 4
  • 33
  • 60
0

Use jQuery .trigger() http://api.jquery.com/trigger/

$('#test').trigger('click');
Jayababu
  • 1,641
  • 1
  • 14
  • 30
0

document.getElementById('test') returns a plain DOM node which doesn't do much. If you use $('#test') you will get the same DOM node, but wrapped in jQuery.

jQuery provides a .click() function on these "wrapped" nodes, which will simulate a click on the button for you:

$('#test').click();

(which is just a shortcut for .trigger('click') - read more here)

is the same as

$(document.getElementById('test')).click();
Community
  • 1
  • 1
Alex McMillan
  • 17,096
  • 12
  • 55
  • 88