Adding a mouse to a link through javascript

Simple quick question ....

I have the following html link:

<a href="http://www.site.com/" onmouseover="" /> 

I have a javascript function in which I want to dynamically inject some onmouseover information into this link. So, let's say, then it becomes like this, for example, if this javascript function is called:

 <a href="http://www.site.com/" onmouseover="alert('howdy')" /> 

any ideas how to do this?

+4
source share
5 answers

The answer was using setAttribute () javascript.

+2
source

Add name attribute and assign onmouseover

 <a href="http://www.site.com/" onmouseover="" name="xxx"/> document.getelementsbyname('xxx').onmouseover = function() { alert('howdy') } 
+5
source

I think you want to say: dynamically change your href attribute information, then you can do it with jquery

 //Write code for prompt box and get value (when mouse-over) $("a[href='http://www.google.com/']").attr('href', 'YOUR_GET_VALUE') 
0
source

If you can use jquery, see http://api.jquery.com/hover/

This is better than changing the attribute directly. Your javascript function can dynamically bind / unbind a mouseover event and make an alert call.

Otherwise, your javascript function will need to dynamically change the attribute, but you will need to go around the browser differences to find the correct element, and then find and change the onmouseover attribute.

0
source

two options:

if it is something small:

 <a href="http://www.site.com/" onmouseover="this.href = 'http://stackoverflow.com'" /> 

if you have something more:

 <script type="text/javascript"> function doSomething(elem) { elem.href = 'http://stackoverflow.com'; } </script> <a href="http://www.site.com/" onmouseover="doSomething(this)">test</a> 

Or as stated earlier: use jQuery or any other structure to make your life a lot easier

0
source

Source: https://habr.com/ru/post/1338082/


All Articles