0

I don't know if my message will be clear but I paste the code here:

HTML

 <header>
    <h1>The Otherside</h1><br>

    <nav>
        <ul id="nav1">
            <li><a href="guitarra.jpg" title="Música">Música</a></li> 
            <li><a href="gandalf.jpg" title="Cinema">Cinema</a></li> 
            <li><a href="mon.jpg" title="Opinions">Opinions</a></li> 
            <li><a href="lleo.jpg" title="Botó 4">Botó4</a></li> 
        </ul>
    </nav>

</header>

I want to select the first link, by title:"Cinema". In JQuery i'm doing this but doesn't work:

window.jQuery("select[title='Cinema']").onclick(function (){
    alert("Works!!!);
});
  • You want to select `a` elements, yet the primary selector is `select`... ? – Rory McCrossan Jan 15 '15 at 16:50
  • @RoryMcCrossan It seems he thinks that `select` says "do select elements that have ..." :) – Ram Jan 15 '15 at 16:52
  • [This is a good example of why you shouldn't copy code that you don't understand...](http://stackoverflow.com/a/3080346/1947286) – apaul Jan 15 '15 at 17:15
  • possible duplicate of [Get element by title jQuery](http://stackoverflow.com/questions/3080339/get-element-by-title-jquery) – apaul Jan 15 '15 at 17:18

3 Answers3

4

Your selector is looking for select elements. Try a instead:

a[title='Cinema']

As Amit pointed out, you have an issue with the click handler -- it's click(), not onclick().

See it in action after these corrections:

$(function() {
  $('a[title="Cinema"]').click(function() {
    alert('You clicked "cinema".');  
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<header>
    <h1>The Otherside</h1><br>

    <nav>
        <ul id="nav1">
            <li><a href="guitarra.jpg" title="Música">Música</a></li> 
            <li><a href="gandalf.jpg" title="Cinema">Cinema</a></li> 
            <li><a href="mon.jpg" title="Opinions">Opinions</a></li> 
            <li><a href="lleo.jpg" title="Botó 4">Botó4</a></li> 
        </ul>
    </nav>

</header>
Cᴏʀʏ
  • 105,112
  • 20
  • 162
  • 194
2

First, its <a> and not <select> and the handler is named click unlike it's DOM APi

jQuery("a[title='Cinema']").click(function (){
    alert("Works!!!);
});
Amit Joki
  • 58,320
  • 7
  • 77
  • 95
0

It is an a element not a select element. Also onclick isn't a thing, but click is. ALSO, you forgot to finish your string in the alert with a ' (single-quote).

$("a[title='Cinema']").click(function(){
    alert('Works!!!');
});
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Ryley
  • 21,046
  • 2
  • 67
  • 81