Assign various elements different values using the same function

Asked

Viewed 1,020 times

2

See, I have 5 IDS(5 DOM elements) and accurate, using Jquery’s text() function, add to each of them a different text, I can do so..

$("#ID1").text("bla");

$("#ID2").text("blabla");

....

There is a smaller, simplified way to do this, if the texts were equal, you could comma the selectors, but they’re not..

I thought of something like:

.text(function() {
$("#ID1") = "bla";
$("#ID2") = "blabla";
});

Anyway, any suggestions, even for good practice and to keep the code simplified and clean.

  • 1

    Where does this text come from? can you have this text in an array to make the most auto-magic code? Another question: Ids differ only by number?

  • 1

    see this example using a basic function: http://jsfiddle.net/z7Leskqb/

  • Alexandre saw my comment above?

  • @Sergio, the text is static, it is not generated randomly, or anyway, but are 5 different texts. You can put it in an array. IDS do not differ by numbers, they are totally different.

  • Interesting @Wallacemaxters, I’ll see better how it works!

2 answers

2

  var addText = function(array, element){
         
        $(element).each(function(i){
            $(this).text(array[i]);
        }); 
    };
    
    $(function(){
      var array = [
          "meu primeiro texto",
          "Meu segundo Texto",
          "Meu terceiro texto",
          "E assim por diante",
       ];
     
        addText(array, '.CLASS');
    });


    addText(array, '[id^=ID]');
.CLASS{
  border:1px solid red;
  height:50px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
<div class="CLASS"></div>
    <div class="CLASS"></div>
    <div class="CLASS"></div>
    <div class="CLASS"></div>
    <div class="CLASS"></div>

  • Look at another almost identical way, but it seems more idiomatic in jQuery: http://jsfiddle.net/nLxn4wpk/

2

You can create a dictionary format object, where the keys are the Divs Ids, and the values are the respective texts. Then just scroll through the dictionary to fill out the Divs:

var textos = {
    "id1" : "bla",
    "id2" : "blabla",
    "foo" : "bar" // , etc
};
$.each(textos, function(chave, valor) {
   $('#' + chave).text(valor);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="id1"></div>
<div id="id2"></div>
<div id="foo"></div>

  • @Wallace Maxters, gave a perfect solution.

  • If the classes make sense, his solution is good yes. But mine has some problem to deserve the vote against?

  • Yes. There are a lot of good and more recommended solutions than this, in the case of the friend, @Wallace Maxters. I’ve been there too. I created a solution, but they gave me a negative, not because I was wrong, but because it wasn’t the best solution. You are moderator, you will understand: http://answall.com/questions/45287/encryptpassword-e-se-logar-php-e-pdo/45304#45304

  • I don’t agree, but ok. About your other answer, here is not the place to discuss. If you want, you can open a discussion on the subject at [goal].

  • The @bfavaretto solution was also cool yes. I would use his idea and launch into a function (because the idea I expressed in the answer was thinking that the operation would be reused, not just used once). For, if the operation were to be carried out only once, there is no need to put it in a function - and therefore its example is fully valid.

  • 1

    The difference is that with Arrays, you can depend on the position of the elements within the DOM and, with Object, you specify which text you want in which place, dynamically!.

  • Right and when do you intend to mark as a solution to your problem? Or None of these answers solves your problem?

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.