0

I'm trying to 'hightlight' a specific div.

Here is my html code

<!DOCTYPE html>
<html>
    <head>
        <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
        <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
        <style type="text/css">div { margin: 0px; width: 300px; height: 80px; background: #FFF; border: 1px solid black; position: relative; }</style>
        <script>
            $(document).ready(function() {
                $("a").click(function () {
                    $("div").effect("highlight", {color:"#FF0000"}, 3000);
                });
            });
        </script>
    </head>
    <body>
        <a href="#id_1"><b>id_1</b></a>
        <br/>
        <a href="#id_2"><b>id_2</b></a>

        <br/><br/><br/><br/>
        <div id="id_1"><h2>id_1 - <a class="altlink" href="#top" name="id_1" id="id_1">Top</a></h2></div>
        <div id="id_2"><h2>id_2 - <a class="altlink" href="#top" name="id_2" id="id_2">Top</a></h2></div>
    </body>
</html>

When I click on the a element with the href #id_1, how can I make it highlight the <div id="id_1"> element?

Thanks !

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Anonymous
  • 25
  • 1
  • 6

4 Answers4

1

Try this:

$('a[href^="#id"]').on('click', function() {
    var sHref = this.href.split('/');
    $(sHref[sHref.length - 1]).effect("highlight", {
        color: "#FF0000"
    }, 3000);
});

Fiddle: http://jsfiddle.net/maniator/4PgC6/


Side note: If using jQuery < 1.7 then do:

$('a[href^="#id"]').click(function() {
    var sHref = this.href.split('/');
    $(sHref[sHref.length - 1]).effect("highlight", {
        color: "#FF0000"
    }, 3000);
});

Fiddle: http://jsfiddle.net/maniator/4PgC6/9/

Naftali
  • 144,921
  • 39
  • 244
  • 303
0

$(document).ready(function() { $("a#id_1").click(function () {
$("div#id_1").effect("highlight", {color:"#FF0000"}, 3000); return false; }); $("a#id_2").click(function () {
$("div#id_2").effect("highlight", {color:"#0000FF"}, 3000); return false; }); });

and the HTML would have an "A" tag which is [code]

a href="#" id="id1"

a href="#" id="id2"

i am ArbZ
  • 69
  • 3
0

Try this:

$('a').click(function() {
    var selector = $(this).attr('href');

    // Highlight div with whatever CSS you like
    $(selector).effect("highlight", {color:"#FF0000"}, 3000);
}

This uses the href attribute of the link as the selector for the .effect() line.

Bojangles
  • 99,427
  • 50
  • 170
  • 208
0

you need to use here like this :

$(document).ready(function() {
    $("a").click(function () {
                    $(this).parent().parent().effect("highlight", {color:"#FF0000"}, 3000);
                });
            });

EDIT : fixed the mistake, put the second .parent() call

LostMohican
  • 3,082
  • 7
  • 29
  • 38