user2888298
user2888298

Reputation: 1

Javascript change onmouseover to click

I have this working code, I need to change to a click instead of mouseover:

var l1OK_WC = false;
var l2OK_WC = false;
function share()
{
   alert('yo');
}
function getIt_wc()
{
   if(l1OK_WC && l2OK_WC)
      window.open('http://google.ca','_self');

   if(!l1OK_WC)
      alert("Click button one");
   else if(!l2OK_WC)
      alert("Click Button two");
}

After this, I have this code:

onmouseover="javascript:l1OK_WC=true;"
onmouseover="javascript:l2OK_WC=true;"

How do I change this part into a click instead of onmouseover. So they need to click instead of onmouseover. I have tried changing to onclick but the script does not work anymore. It stays false and always displays the message of "click button one"

Upvotes: 0

Views: 145

Answers (4)

Jeroen
Jeroen

Reputation: 1002

Replace:

onmouseover="javascript:l1OK_WC=true;"
onmouseover="javascript:l2OK_WC=true;"

by

onclick="javascript:l1OK_WC=true;"
onclick="javascript:l2OK_WC=true;"

For more info see:

onClick and onMouseOver

Using an event listener:

<input type="button" value="Button 1" id="myButton1"/>
<input type="button" value="Button 2" id="myButton2"/>

<script>
    myButton2.addEventListener("click", function() { alert('button 1 clicked!'; }, false);
    myButton2.addEventListener("click", function() { alert('button 2 clicked!'; }, false);
</script>

Upvotes: 1

WebDev67
WebDev67

Reputation: 34

You can use either the HTML this way: your-element onclick="JavaScript code"

or use JavaScript to fire it... object.onclick=function(){JavaScript Code};

If it's in a button you will have to wrap the button around it :-)

Hope this helps...

Upvotes: 0

SomeKittens
SomeKittens

Reputation: 39522

You can change the HTML attribute to onclick, but that's not really best practice. Instead, why not attach an event handler to the elements in question?

Something along the lines of:

// Assuming you've already grabbed the elements and put them in the variable `myElements`
myElements.addEventListener('click', function() {
    l10K_WC = true;
});

This lets you centralize your code (so you only need to make one change, instead of many throughout your HTML, as well as helps caching. For more information, see here: https://softwareengineering.stackexchange.com/a/86595/54164

Upvotes: 1

Seano666
Seano666

Reputation: 2238

Unless I'm misunderstanding, it's just this simple...

onclick="javascript:l1OK_WC=true;"
onclick="javascript:l2OK_WC=true;"

Upvotes: 1

Related Questions