0

I have this form:

<form action="javascript:;" class="iceFrm" enctype="application/x-www-form-urlencoded" id="frmMainMenu" method="post" onsubmit="return false;">
.....
</form>

I do not understand why this script:

<script type="text/javascript">
$(document).ready(function () {
    $("#frmMainMenu").$("#menuPopupAP").$("#menuItem0").$("#out").css('padding','0 0 0 29px');
});
</script>

says:

$("#frmMainMenu") is null

in Firebug.

UPDATE: This is the html element (from the above form) I want to change the padding on:

<span id="frmMainMenu:menuPopupAP:menuItem0:out" class="iceOutTxt iceMnuItmLabel graMenuPopupMenuItemLabel">My Applications</span>

Update 2:

I've forgot to mention that this span is within a menu, so basically it's hidden normally and displayed only when hovering on some div... Does jQuery finds it even if it's not displayed?

Do you know why?

Cristian Boariu
  • 9,461
  • 13
  • 89
  • 160

6 Answers6

2

I'm not sure exactly why you're getting the null error, but if your intent is to apply that style to those four specific elements the syntax should be:

$("#frmMainMenu, #menuPopupAP, #menuItem0, #out").css('padding','0 0 0 29px');

If (and I can't tell as we've been giving an insufficient markup sample) those four elements are successive nodes in a hierarchy (i.e. #out is a child of #menuItem is a child of #menuPopupAp is a child of #frmMainMnenu) then remove the commas between them.

EDIT colons in an id? That'll cause problems with jQuery selectors. Try using an underscore instead?

Alnitak
  • 325,660
  • 70
  • 395
  • 481
  • IDs *can* contain colons, it's valid, but jQuery can't select IDs with colons like that, because the `:` is read as a selector. You need to do `[id="value"]` instead. – gen_Eric Mar 23 '11 at 16:47
1

Update after your question update:

$('#frmMainMenu\3AmenuPopupAP\3AmenuItem0\3Aout').css('padding','0 0 0 29px');

You cant use the : in your selector but you can use the hexadecimal equivalent - \3A

There are few other workarounds on this SO thread.


Are your trying to search the children of frmMainMenu? try this instead:

$(document).ready(function () {
            $("#frmMainMenu").find("#menuPopupAP")
                             .find("#menuItem0")
                             .find("#out")
                             .css('padding','0 0 0 29px');
});
Community
  • 1
  • 1
Geoff Appleford
  • 18,170
  • 4
  • 61
  • 85
0

It's not that - it's one of the children that is null. You need to change your script to something like this

<script type="text/javascript">
 $(document).ready(function () {
            $("#frmMainMenu #menuPopupAP #menuItem0 #out").css('padding','0 0 0 29px');
        });
        </script>
Chris Dixon
  • 9,117
  • 5
  • 34
  • 67
0

Surely you don't mean to use # each time (otherwise you could just use #out, but it sounds like it should be something like

$("#frmMainMenu .menuPopupAP .menuItem0 .out").css(...

silkcom
  • 472
  • 1
  • 5
  • 14
0

Can you post what do you need to accomplish? from your selector, I think you are taking a hard selector approach to interact with your DOM.

Mohammed Swillam
  • 8,822
  • 3
  • 34
  • 45
0

$('#frmMainMenu:menuPopupAP:menuItem0:out').css('padding','0 0 0 29px');

$('span[id="frmMainMenu:menuPopupAP:menuItem0:out"]').css('padding','0 0 0 29px');

You need to use the [id="value"] syntax, because in a jQuery selector the : can be used as a selector, so it doesn't realize that the IDs can contain them.

gen_Eric
  • 214,658
  • 40
  • 293
  • 332