$(document).ready(function(){ $(...">

Href is always "undefined"

In warning mode, I always get undefined ... why?

<script type="text/javascript">

$(document).ready(function(){
    $("#menu ul li").click(function() {
        var path = $(this).attr("href"); 
        alert (path);
        $.get(path, function(data) { $("#texte").html(data); });
        return false;
    });
});

</script>

</head>

<body>

<div id="wrapper">

<div id="menu">
  <ul>
    <li><a href="pulse/data/blocks/intro.html">Intro</a></li>
    <li><a href="pulse/data/blocks/presentation.html">presentation</a></li>
    <li><a href="pulse/data/blocks/pourquoi.html">pourquoi ?</a></li>
    <li><a href="pulse/data/blocks/forfaits.html">forfaits</a></li>
  </ul>
</div>
+3
source share
8 answers

You need to select a tag a.

var path = $('a', this).attr("href"); 
+9
source

You need to select an item:

$(document).ready(function(){
    $("#menu ul li").click(function() {
        var path = $(this).find("a").attr("href"); 
        alert (path);
        $.get(path, function(data) { $("#texte").html(data); });
        return false;
    });
});
+2
source

LI, A.

:

$("#menu a")
+2

href (li). :

$("#menu ul li a").click(function() { 
+2

href li. , href a. :

$("#menu ul li a")

, , ul li.

+2

The problem is that it $(this)refers to the element <li>, not the link. You can fix this using the following selector:

var path = $("a", $(this)).attr("href"); 
+2
source

you missed the tag.

$("#menu ul li a").click
+1
source

Your jQuery is referencing the wrong element: Instead of referencing the UL Li element, you need to reference the UL LI A element, as shown below:

$(document).ready(function(){
    $("#menu ul li **a**").click(function() {
        var path = $(this).attr("href"); 
        alert (path);
        $.get(path, function(data) { $("#texte").html(data); });
        return false;
    });
});
+1
source

All Articles