Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Can not target a textarea placeholder?

My code runs through all components in my form, looping through each one.

// selects all form elements
var $inputs = $('form, #create-advert-form').find("input, select, button");

// loops through each element
$inputs.each(function () {

If required, the components text is modified to add a - '•'. This all works!

    // if required
    if ($(this).prop('required')) {
        // add • to current text
        var $currentText = $(this).siblings('label').text();
        var $updatedText = $(this).siblings('label').text($currentText + ' •');

This is where the problem starts. Trying to select a textarea placeholder returns the error: Uncaught TypeError: $(...).is(...).attr is not a function(anonymous function) @ scripts.js:111n.extend.each @ jquery-1.12.0.min.js:2n.fn.n.each @ jquery-1.12.0.min.js:2(anonymous function) @ scripts.js:103

        // add • to textarea placeholder
        var $currentTextArea = $(this).is('textarea').attr('placeholder');
        var $updatedTextArea = $(this).is('textarea').attr('placeholder', $currentTextArea + ' •');
    }
});

Thanks for any suggestions!

1 Answer

Steven Parker
Steven Parker
243,318 Points

A couple of things strike me right off:

  • $.is() returns true or false, so you can't chain it with $.attr(),
  • 'texarea' is not a valid element selector, you might want .is('[type="textarea"]').

Thanks! I'll try and fix it up now :)