Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterating through all Textareas with Javascript (jQuery)

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 replacement 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!

like image 994
Jeff Avatar asked Jun 01 '11 13:06

Jeff


1 Answers

$(function(){
    $("textarea").each(function(){
        this.value = this.value.replace("AFFURL",producturl);
    });
});

See a working demo

like image 63
rahul Avatar answered Oct 06 '22 00:10

rahul