assassin
assassin

Reputation: 21169

error selecting div element with a particular id - beginner at jQuery, javascript

I am playing around with selecting elements in the DOM using jQuery, and I tried the following:

$(document).ready(function() {

      $("a").toggle(function(){
        $("div[@id=SomeID]").hide('slow');
        },function(){
        $("div[@id=SomeID]").show('fast');
       });

});

And in the html source, I do have this section:

<div id="SomeID">

      <!-- div code -->

</div>

However, when I click on an anchor tag I get the following error:

Syntax error, unrecognized expression: div[@id=SomeID]

Any ideas as to what is going wrong here? I'm a beginner at jQuery and javaScript, so would appreciate any help.

Upvotes: 0

Views: 882

Answers (2)

Sushanth --
Sushanth --

Reputation: 55750

You are using the Wrong selector.. Id has to be prepended with a #

These are valid

$("#SomeID").hide('slow'); //  element with id SomeID , can be any element
$("div#SomeID").hide('slow'); //  Div with id SomeID
$("div[id=SomeID]").hide('slow'); //  Div with id SomeID
$("div[id^=SomeID]").hide('slow'); //  Div id that starts with SomeID
$("div[id*=SomeID]").hide('slow'); //  Div id that has SomeID in its id attribute
$("div[id=SomeID]").hide('slow'); //  Div id that ends with SomeID

Upvotes: 1

David Thomas
David Thomas

Reputation: 253318

To select an element by its id in jQuery, use the same syntax as in CSS:

$('#someID');

To use the attribute-equals selector format then use:

$('div[id="someID"]');

References:

Upvotes: 4

Related Questions