0

I have the following:

<div class="modal hide fade" id="adminDialog" style="display: none;">
    <div class="modal-header">
        <a data-dismiss="modal" class="close">×</a>
         <h3 id='dialog-heading'></h3>
    </div>
</div>

I created this variable:

dialogDiv = $('#adminDialog');

I would like to change the text of the heading. If I want to do this do I need to use $('#dialog-heading').text() or is there a shortcut way that is based on the dialogDiv variable?

Chuck Norris
  • 15,207
  • 15
  • 92
  • 123

5 Answers5

1

If it has to be based on the dialogDiv then this is the best one I know

$("h3", $("#adminDialog")).text('new text');

Or

$("h3", dialogDiv).text('new text');

This selector tell you to find a h3 within the scope of #adminDialog.


But you are using an id for the div, so no selector can top the direct selection. Like

$('#dialog-heading').text('new text');
Starx
  • 77,474
  • 47
  • 185
  • 261
  • h3 comes first? I never saw this kind of selector before. Also can I just replace $("#adminDialog") with dialogDiv? –  Apr 16 '12 at 07:58
  • So $("h3", dialogDiv).text() would be okay? That's looks very clean to me :-) –  Apr 16 '12 at 08:04
  • @MarieJ, Yes it is OK and very clear way to select within a scope. – Starx Apr 16 '12 at 08:05
0

You can use either dialogDiv or $('#adminDialog') as they refer to same object. It only works if the dialogDiv is present in your current scope (meaning, if you've assigned it in a function, it is only available within that function)

Starx
  • 77,474
  • 47
  • 185
  • 261
Nanne
  • 64,065
  • 16
  • 119
  • 163
  • 1
    A lookup by ID will be quicker than one based in `dialogDiv` though since ID lookups are most likely `O(1)`. – ThiefMaster Apr 16 '12 at 07:56
  • But what I was meaning is what's the cleanest way for me to set the H3 title. I think it looks a little more clean if I can use dialogDiv as a base but I am not sure how to do that. –  Apr 16 '12 at 07:56
  • define clean, it's not 'written in stone'. If you need to use it for many things, you can save it in a variable, but if you only do one thing, just use the jquery directly, so you don't have all teh variables. And about the speed: you are looking at minor, minor, microoptimalization here, don't bother with that please. – Nanne Apr 16 '12 at 07:58
0

As long as you have access to dialogDiv within the current scope, it is even prefered to use dialogDiv.find("#dialog-heading").text("new text"), as it wouldn't have to go through the entire DOM again, thus slightly more performent.

Christofer Eliasson
  • 32,939
  • 7
  • 74
  • 103
0

It highly depends on your needs.

Since your element has an ID, $('#dialog-heading') will be the fastest way to access it.

However, if you want to access "the <h3> inside #adminDialog", better use adminDialog.find('h3')

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
0

Assuming you only have one h3 child element in your div#adminDialog, you can also use a CSS selector:

$('div#adminDialog h3').text("bla bla")
frno
  • 2,641
  • 2
  • 18
  • 19