0

I need to get the next textarea, but I'm not being able with next or even find.

Sample code HTML:

<div>
    <div class="guides_chapters_button" onclick="surroundtest('[center]', '[/center]');">Center</div>
    <div class="guides_chapters_button" style="text-decoration: underline;" onclick="surround('[u]', '[/u]');">u</div>
</div>
<textarea class="guides_chapters_textarea" id="textareamatch" name="matchupm" rows="7" cols="25"></textarea>

JS:

window.surround = function surround(text2,text3){
$("#textareamatch").surroundSelectedText(text2, text3);
}
function surroundtest(text2,text3){
var c = $(this).parent().next('textarea');
c.surroundSelectedText(text2, text3);
}

JS FIDDLE: http://jsfiddle.net/qmpY8/1/

What I need working is surroundtest, the other is an example working but using the id. I would love to replace that one because Im usinc cloned objects.

4

2 回答 2

2

中的this语句surroundtest适用于window对象而不是元素。您应该做的是将函数定义更改为:

function surroundtest(element, text2,text3){
    var c = $(element).parent().next('textarea');
    ...
}

和相应的HTML:

<div class="guides_chapters_button" onclick="surroundtest(this, '[center]', '[/center]');">Center</div> 
于 2013-08-13T13:52:12.873 回答
0

如果这是您要使用的 HTML,则 .closest() 也可用于获取 textarea 元素。如下所示:

var c = $(element).parent().closest('textarea');
于 2013-08-13T14:01:10.513 回答