Getting value of ID from class using jQuery?

class, jquery

Solution

You are looking for attr() or prop().

Since you want to obtain the value of the `id` HTML attribute, I would emphasize that and go with `attr()`:

var id = $(".cp._check").attr("id");

Your class selector is also incorrect: since your element exposes both the `cp` and the `_check` classes, it should be `.cp._check` instead of `.cp _check` (which would match the `<_check>` elements that are descendants of elements that expose the `cp` class).

Problem

How would you retrieve the value of the ID tag using its class name in jQuery. I am trying the following code but it returns value `undefined`. ``` <html> <body> <textarea id='5' class='cp _check'>sometexthere</textarea> </body> <html> ``` And calling it using the following function: ``` function updatecontent() { var check = $('.cp _check').('#id').val(); alert(check); } ``` The `alert` box returns value as `undefined`.

Original source