1

I'm trying to write code something like this:

$('[id^=myitem_]').data(
    'powq',
     $([
         '<p><b>Description:</b></br>'+$(this).find("input[name=description]").val()+'</p>',
         '<p>Some other data:</p>'
     ].join(''))
);

As you can see inside 'value' i want to catch object inside [id^=myitem_], but i cannot. In this case every time i get first object in whole document. I think here, inside 'value', $(this) mean window or whole document.

Any idea..?

Anujith
  • 9,370
  • 6
  • 33
  • 48
Kolesar
  • 1,265
  • 3
  • 19
  • 41

2 Answers2

4

What this refers to depends on the context where you executed that code. It's likely that it refers to window though.

If you want it to refer to each [id^=myitem_] element, then you have to use .each [docs] to iterate over all selected elements:

$('[id^=myitem_]').each(function() {

    $(this).data(
        'powq',
         $([
             '<p><b>Description:</b></br>'+$(this).find("input[name=description]").val()+'</p>',
             '<p>Some other data:</p>'
         ].join(''))
    );
});
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
0

To access div elements with the ID myitem:

$("#myitem")

I might have completely misunderstood your question though.

Daniel Figueroa
  • 10,348
  • 5
  • 44
  • 66