-1

I am trying to find a cell in a table that has a class of 'empty', i am then using a bit of code to find the id(cell number) so i can the find out what cells are next too it.

just too see if it works, i am trying:

console.log($('.empty').attr('id'));

but Firebug just returns 'undefined'

each cell has the class of 'box' and only one has empty as well so 'box empty'.

Any asssitance would be much appreciated.

4
  • 2
    Please show your markup. Commented Feb 12, 2012 at 21:28
  • $('.empty') may return array of DOM elements. In that case, array itself does not have attribute Commented Feb 12, 2012 at 21:28
  • $(document).ready(function(){ $('.empty').each(function() { console.log( $(this).attr('id') ); }); }); jquery above, html for cell <td id="B" class="leftbox" name="empty" ondragover="allowDrop(event)" ondrop="drop(event,this.id)"> </td> Commented Feb 12, 2012 at 21:38
  • @nmyster You need attribute selector, not class. the name is empty, not the class! See my updated answer. Commented Feb 12, 2012 at 21:42

2 Answers 2

2

Probably the DOM isn't ready. Put the code inside the on DOM ready event:

$(function(){console.log($('.empty').attr('id'));});
//or
$(document).ready(function(){console.log($('.empty').attr('id'));});

Update:

Based on the Markup you wrote, the selector you need is attribute selector not class:

<td id="B" class="leftbox" name="empty" ondragover="allowDrop(event)" 
    ondrop="drop(event,this.id)"> </td>

$(function(){console.log($('input[name="empty"]').attr('id'));});
Sign up to request clarification or add additional context in comments.

1 Comment

$(document).ready(function(){ console.log($('.empty').attr('id')); } each cell has class of 'box' and empty cell has class of 'box empty' and each cell has an ID that is its number of cell.
1

$('.empty') return an array of dom element, so you must use a loop (for, each...) to analyse results

$('.empty').each(function() {
   console.log( $(this).attr('id') );
}

If you are sure there is only one element, you can use :

console.log( $('.empty:first').attr('id') );

1 Comment

He is sure there is only one. but the problem he mixed up class with name. see my answer and his comment on his question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.