1

I am trying to get the jquery equvaliant of this javascript

var id = $(this).parent().parent().parent().attr("id");
document.getElementById(id).getElementsByClassName("addcomment")[0].style.display = 'block';

but its not working

$('#+id+' '.addcomment').css('display','block');

Any suggestions ?

Yahoo
  • 4,093
  • 17
  • 59
  • 85
  • 2
    Your string concatenation is wrong. – Jivings May 18 '12 at 10:19
  • If you're using a code editor, then these sort of errors are caught/identified very easily by the syntax highlighting... – nickf May 18 '12 at 10:20
  • possible duplicate of [How to use javascript variables in jquery selectors](http://stackoverflow.com/questions/5891840/how-to-use-javascript-variables-in-jquery-selectors) – Felix Kling May 18 '12 at 10:20
  • Might be worth posting some html as you are getting some very presumptuous answers – musefan May 18 '12 at 10:21

3 Answers3

4
$('#' + id + '.addcomment').css('display','block');

as a sidenote in the page you should have only one element with that id, so

$('#' + id ).css('display','block');

should works too (of course only if classname it's not necessary to target it, since this is a different selector)

Fabrizio Calderan
  • 120,726
  • 26
  • 164
  • 177
1
$('.addcomment', '#' + id).css('display','block');

or just simple

$('#' + id).css('display','block');
thecodeparadox
  • 86,271
  • 21
  • 138
  • 164
1

I think you need to get the child elements based on their class. So try this.

$('#' + id ).find('.addcomment').show();
Mathew Thompson
  • 55,877
  • 15
  • 127
  • 148
indika
  • 11
  • 1