Jan Ariel San Jose
Jan Ariel San Jose

Reputation: 720

How to target a textarea(child) inside a div(parent)

So I wanted to add a name attribute on a textarea inside the div.

So my div has an id of textAreaEmail.

Here's the syntax I've tried:

$(textAreaEmail).removeAttr('hidden');
$(textAreaEmail).children('textarea').attr('name', 'body');

Here's my HTML, I used Form Helpers.

<div id="textAreaEmail">
    <div class="form-group <?php echo ($form->error('body') ? 'has-error' : ''); ?>">
        <?php echo label_tag('Body', 'body', true, array('class' => 'control-label'), ''); ?>
        <?php echo textarea_field('body', $form->getField('body'), array(
            'class' => 'form-control' . ($form->error('body') ? ' is-invalid' : ''),
            'required' => true,
            'id' => 'tinymce'
        )); ?>
        <p class="help-block invalid-feedback"><?php echo ($form->error('body') ? $form->error('body') : ''); ?></p>
    </div>
</div>

Upvotes: 0

Views: 712

Answers (3)

Johannes
Johannes

Reputation: 67799

You forgot the "#" symbol for the ID and the quotes in your two selectors, so...

$(textAreaEmail).removeAttr('hidden'); 

...should be

$('#textAreaEmail').removeAttr('hidden');

Upvotes: 0

Hanif
Hanif

Reputation: 3795

Try following way but I'm not sure where you have hidden attribute but I followed your question:

$("#textAreaEmail").removeAttr('hidden').find('textarea').attr('name', 'body'); // If hidden attribute under parent div
$("#textAreaEmail").find('textarea').removeAttr('hidden').attr('name', 'body'); // If hidden attribute under textarea

Upvotes: 0

void
void

Reputation: 36703

If you want to set attribute name to the value body then do:

$("#textAreaEmail").find('textarea').attr('name', 'body');

You need to use # to use id as a selector.

Upvotes: 2

Related Questions