1

I'm very new to jQuery BUT I've got the basics down... At least I think I do? I'm just not sure how to put them together for them to actually work.

Example:
IF (div with ID of #dynamicChat) is present on the page. Then (div with class of .prdSection) height is 25px. Else do nothing

What I have so far:

$('#dynamicChat'){
$('.prdSection').css('height', '25px');
}

I don't think I'm far off... Saying that I probably am :) Any help would be great!

*Side question - If I'm referring to a Div on a page, what would I call it? An Element?

Thanks :)

Nick
  • 2,261
  • 5
  • 33
  • 65

4 Answers4

9
if ($('#dynamicChat').length > 0) {
    $('.prdSection').css('height', '25px');
}
mathius1
  • 1,381
  • 11
  • 17
1

You don’t need the “> 0”, you can simply write:

if ( $('#myElement').length ) {
// it exists
}

if ( $('#dynamicChat').length ) {
$('.prdSection').css('height', '25px');
}
Aleksandr M
  • 24,264
  • 12
  • 69
  • 143
immazharkhan
  • 395
  • 2
  • 12
0

No need of jQuery to check if that element exist :

if(document.getElementById('dynamicChat')){
    $('.prdSection').css('height', '25px');
}
Karl-André Gagnon
  • 33,662
  • 5
  • 50
  • 75
0

If (div with ID of #dynamicChat) is present on the page. Then (div with class of .prdSection) height is 25px, else do nothing

   <script type="text/javascript">
        if ($('#dynamicChat').is("visible")) {
            $('.prdSection').css('height', '25px');
        }
    </script>
Ikbel
  • 1,817
  • 1
  • 17
  • 30
  • 1
    Your answer could be improved by adding more information on what the code does and how it helps the OP. – Tyler2P Mar 12 '22 at 12:02