tags:

views:

195

answers:

4

Looking for some assistance with my jQuery code.

I am creating a modal box, using the simplemodal plugin

I am managing to create a modal box on the click of an element, which contains a form....I would like the form to have focus on the textarea field when it appears, but am unsure of how to achieve this..

Here is my current code..

    $('#showModal a').live('click',(function(){
      // Build Modal Box Container And Add to Page      
        $('<div id="osx-modal-content">\
            <div id="osx-modal-title"><h2>Please explain this song.</h2></div>\
            <div id="osx-modal-data"> \
            <p class="loaderGif"> <img src="http://localhost:8500/mxRestore/images/ajax-loader.gif"&gt; </p>\
         </div>\
         </div>').appendTo('body');

         //Set Modal Box Options                
        $("#osx-modal-content").modal({
                // OPTIONS SET HERE
                overlayClose:true,
                onOpen:OSX.open,
                onClose:OSX.close
            });

         // I have a hidden form on the page...grab its HTML!!
         var modalForm = $('#addTmWrapper').html();

         // Dynamically build a text area and add to the form...
         // The text area from my hidden form wont show properly for some reason.....maybe a coldfusion issue

         var textField= ''
         textField+= '<textarea name=' + '"sMessage"' + 'id="sMessages"' + 'cols="62"' + 'rows="3"' + 'class="textarea word_count">' + '</textarea>'

         // Add hidden form to modal box, add textarea to form..
         // Then show it                    
         $('#osx-modal-data').html(modalForm ).find('#addTmForm')
                .prepend(textField)
                .show();
    return false
}));

Wondering how I can make the textarea have focus when the modal box appears?

Any help would be appreciated, thanks

+1  A: 

To give focus to a form element, use focus().

$('#myElement').focus()
patrick dw
A: 

Just before return false;, add $('#sMessages').focus(); (see jQuery docs).

kevingessner
yes, have tried this before, but doesnt seem to work in this instance, unsure why
namtax
Hm. You may want to try .prepend($(textField)), rather than just .prepend(textField) - that'll have jQuery create the element first, then insert it. It might help.Alternatively, you could put the $('#sMessages').focus(); in a setTimeout block of a millisecond or two. The browser might need a moment before it can actually find the new textarea.
kevingessner
A: 

use

$('selector').trigger('focus');

instead of

$('selector').focus();

( $(...).focus(function() {}) binds a function to focus event not doing focus.)

takpar
not working either..strange, maybe some quirk with the modal box
namtax
@takpar - `focus(function(){})` binds. `focus()` triggers.
patrick dw
A: 

try this:

var aSet = $('selector');
aSet[0].focus();

the focus above applies to Element not jQuery Object.

takpar
also, doesnt seem to be working unfortunately
namtax