Jesper, in your first example, you were passing the string "this", not
the keyword. The keyword "this" is a reference to the button (in that
case), and the string "this" is useless for that purpose. In your
second example, you are creating a wrapped set, accessing the first
element in that set, and then passing that first element in to a new
wrapped set, which is effectively twice the processing that you need
to access those elements. Try this instead:

$("button.primary").each(function(){
  // To access the text
  var button = $(this);
  var text = button.text();

  // To set new text....
  button.text("Some new foo bar text");
});

$(...).html() returns the innerHTML, while $(...).text() returns the
text content. As you have no nested tags inside your buttons
(according to your sample code), you should be able to use .text()
without any issues.

Hope that helps.

On Aug 27, 1:35 pm, Jesper Rønn-Jensen <jespe...@gmail.com> wrote:
> Solution:
>
> $($("button.primary")[0]).text()
>
> -- or should we call this a workaround ?
>
> Why will I have to wrap the element in another jQuery function call?
>
> /Jesper
>
> On Aug 27, 5:26 pm, Jesper Rønn-Jensen <jespe...@gmail.com> wrote:
>
> > I'm trying to read (and then change) the text on two buttons in my
> > user interface:
>
> > <button class="primary approve">Approve selected</button>
> > <button class="primary reject">Reject selected</button>
>
> > Now depending on how many checkboxes (elsewhere) are selected, I want
> > to change the text to something like "Approve 4 selected"
>
> > I'm tried in vain to acess:
> > $("button.primary").each(function(){
> >   console.log( $('this').text() );//fails
> >   console.log( $('this').html() );//fails
>
> > });
>
> > and for completeness sake I also tried $(this).value -- but of course
> > it returns the .value attribute of the button.
>
> > 2 questions:
>
> > How do I access the text and change it?
> > And where is that documented -- link wanted :)
>
> > Any help appreciated :)
>
> > /Jesper Rønn-Jensenwww.justaddwater.dk

Reply via email to