DijkeMark
DijkeMark

Reputation: 1306

jQuery can't find div with specified id

I am using jQuery to find a div with a specific id, except it return null. This is strange since trying to find other divs works just fine.

Jquery

$(document).ready(function()
{
    //Finds parent div without a problem
    console.log($('#next-sheet'));

    //Returns null, can't find the div I really need
    console.log($('#start-course'));

    //Also returns null
    console.log($('#next-button'));

    //Still returns null, can't find the div
    console.log($('#next-sheet #start-course'));
});

HTML

<div id='next-sheet'>
    <a href="/courses/start/<?php echo $data['Courses']['Course']['id']; ?>" class='right disable-hover'>
        <div class='button' id='start-course next-button' data-callbackdata="<?php echo $data['Courses']['Course']['name']; ?>" >Start Course</div>
    </a>
    <div class="clear"></div>
</div>

This is very strange, I am using the .ready function, so the html is available, because it can find the parent div. But when I try to find the child div, it can't find anything.

jQuery is loaded correctly, same as all the other stuff. It's all just static HTML. I am using CakePHP, but that should be not the problem.

Is there something I am missing here?

Upvotes: 0

Views: 4240

Answers (4)

elrado
elrado

Reputation: 5272

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").

id='start-course next-button' is NOT OK. Try with start-course_next-button

If you want to find control by more than one "name" use classes

<div class="class1 class2"/>

This should be OK

   console.log($('.class1'));
   console.log($('.class2'));
   console.log($('.class1.class2')); 

Upvotes: 2

Venkat
Venkat

Reputation: 115

You can also try "End with" selector

$("element[id$='txtTitle']")

ie

$("div[id$='next-button']")

Upvotes: 0

Adil Shaikh
Adil Shaikh

Reputation: 44740

You can't have multiple ids for same element -

id='start-course next-button'  // this is wrong

Although you can have multiple classed like this -

class='button start-course next-button' // you will need to replace # with . 

Upvotes: 1

Felix
Felix

Reputation: 38102

Since id is unique, you can only have one id for an element, so use:

id='start-course'

or:

id='next-button'

instead of:

id='start-course next-button'

Upvotes: 6

Related Questions