Canette De Fanta Canette De Fanta - 8 days ago 5
HTML Question

Execute javascript after clicked on a link

I want to execute a javascript after a specific link was clicked on my page. I found this :

<a href="#" id="myHref">Click me</a>

$("#myHref").on('click', function() {
document.getElementById(".myDiv").style.flexGrow = "5";
window.location = "http://www.google.com";});`


However, my "a" tag doesn't have an id and I can't edit the HTML :

<a class="position_top align_center" title="anglais" href="blabla" lang="en-US">


Can I use the title as a selector like this ?

$("blablaanglais").on('click', function() {
document.getElementById(".myDiv").style.flexGrow = "5";
window.location = "http://www.google.com";});`


Or there is another way to do it ?

Thanks in advance.

Answer

You can access the title selector with square brackets.

$("a[title='anglais']").on(....)

A good reference on jQuery selectors can be seen here http://www.w3schools.com/jquery/jquery_ref_selectors.asp