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');

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!