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..?

2 Answers 2

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(''))
    );
});
Sign up to request clarification or add additional context in comments.

Comments

0

To access div elements with the ID myitem:

$("#myitem")

I might have completely misunderstood your question though.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.