April 16, 2012
Is there a shortcut way to refer to elements in my DIV?
Question by Marie J
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?
Answer by Starx
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');