开发者

finding text value after a certain div using jQuery

To save me a lot of work I'd really like to get the value that is next to a div in my form ( which has been automatically generated by Django formsets )

an example of开发者_Go百科 the output looks as follows:

<div class="grid_43 suffix_2">
    <input type="text" name="reward_set-0-for_every" id="id_reward_set-0-for_every">
</div>
<br />
Help text for this particular field

How can I get this value that is after the <br />

I've tried using a few methods such as .prev() .next() different selectors, and now I am all out of ideas.

Any suggestions?


http://jsfiddle.net/r2SnQ/

$('.grid_43').parent().text();


If you want to target the specific text node, try the following

var text_contents = $('.grid_43').parent()
             .contents()
               .filter(function (i, node) {
                  return node.nodeType === 3 && node.nodeValue.replace(/\s|\r/g,'') !== '';
               });

this will return an array of non-empty text nodes, from which you can select the required text you want.

In your case use text_contents[0].nodeValue to get the required text.

This method will be helpful if you've other textual content inside or outside the div.

http://jsfiddle.net/m7CkB/1/


$('#id_reward_set-0-for_every').parent().next().text()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜