开发者

accessing html element by?

I have textarea and I want to bind function textareaCount jquery plugin, to limit input character size.

As I use it in several p开发者_如何学编程laces in my application, I cant use on Id then I generarte random Id:

<% int randomId = new Random().Next(10000, 99999); %>
<textarea id="textarea-message-<%:randomId%>"></textarea>
<script>
$("#textarea-message-<%:randomId %>").textareaCount(...){...}
</script>

Can I have better solution for this situation?

Edit: I load usercontrols via ajax into page. so in the page there textareas binded and new textareas to bind.


You could add a class attribute to the textarea control and bind to that e.g.

<textarea class="countable" id="textarea-message-<%:randomId%>"></textarea>

<script>
$(".countable").textareaCount(...){...}
</script>

Dare I ask what the random thing is about?


Use class instead of id for all the textarea elements and bind that class with the textareaCount jQuery plugin function.


here my solution with class accessor and for multiple loading with ajax:

function doCountable(){
    var textareaToBind = $(".countable");
    $(textareaToBind).each(function(){
           $(this).removeClass("countable").addClass("countabled");
           $(this).textareaCount(...){...}
    })
}

I call doCountable() when page load and after ajax call

any correction or suggestion?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜