I would like to perform something with all my text areas, however selecting all of them with $("textarea")
wont do. Here's what I am trying to do in Pseudo code:
for each textarea do
alert(textarea.val)
In my case, I need to do a Word replace开发者_StackOverflow中文版ment in all of my textareas, so I thought doing it with iteration would be a lot cleaner, however I cant figure out how to do it.
Here is what I currently do, which is very tedious:
var txtcode = $("#txt_banner1").text().replace("AFFURL",producturl);
$("#txt_banner1").text(txtcode);
var txtcode = $("#txt_banner2").text().replace("AFFURL",producturl);
$("#txt_banner2").text(txtcode);
... and on and on....
Thanks!
$(function(){
$("textarea").each(function(){
this.value = this.value.replace("AFFURL",producturl);
});
});
See a working demo
You can use .each
to iterate over all of the textarea
elements and do what you need to with them via jQuery.
Live Demo
$('textarea').each(
function(){
alert($(this).val());
}
);
Reference
instead using id do below
$("textarea").each ( function () {
// whatever u want
});
http://jsfiddle.net/PKenB/
//alternative 1
$(function() {
$('textarea').each(function() {
alert($(this).text())
})
})
//alternative 2 only those who has specific class
$(function() {
$('textarea.myTextArea').each(function() {
alert($(this).text())
})
})
//alternative 3, handpicked textareas
$(function() {
var handPicked = ['#2', '.myTextArea']
for (x = 0; x < handPicked.length; x++) {
alert($(handPicked[x]).text())
}
})
精彩评论