Sebsemillia
Sebsemillia

Reputation: 9476

Target text with jQuery without attributes

I need to target a plain text within a form via jQuery, so that I can wrap a label around it. The form itself is created by an external plugin which I can't edit. As the id's, values and names of the inputs might be changed by the plugin, I can't take these as selectors. That's what makes it difficult for me.

Here is an example code:

<form method="post" action="#" name="process213">
    <input id="page_action327" type="hidden" value="confirmation" name="page_action">
    <input id="special108" type="hidden" value="coupon" name="special">
    Text
    <input id="coupon_code" type="text" maxlength="20" name="coupon_code">
    <input type="submit" value="Einlösen" title="Einlösen">
</form>

I need to target "Text". Does anyone have an idea how to achieve that without using the above mentioned attributes? Thank you!

Upvotes: 0

Views: 76

Answers (1)

antyrat
antyrat

Reputation: 27765

You need something like this:

$('form[name=process213]').contents().filter(function(){
    return this.nodeType === 3 // Node.TEXT_NODE
}).wrap('<label />');

Update:

and then to delete empty labels you can do just:

$( 'form[name=process213] label:empty' ).remove();

Upvotes: 2

Related Questions