javascript - How could I add the same string on different paragraph multiple time on the same HTML page? - Stack Overflow

I wish to know the best way to write only once the same thing and repeat inside the same page. For exam

I wish to know the best way to write only once the same thing and repeat inside the same page. For example:

<div>
    <p id="description1"></p>
</div>
<div>
    <p id="description1"></p>
</div>
--

I wish to write only one time the description1 inside the body. I think this could be achieved using the DOM.

I wish to know the best way to write only once the same thing and repeat inside the same page. For example:

<div>
    <p id="description1"></p>
</div>
<div>
    <p id="description1"></p>
</div>
--

I wish to write only one time the description1 inside the body. I think this could be achieved using the DOM.

Share Improve this question asked Mar 17, 2015 at 12:01 NineCattoRulesNineCattoRules 2,4587 gold badges42 silver badges91 bronze badges 2
  • Give them the same class and different IDs. Then, as answered by neuhaus use getElementsByClassName to control it – Eda190 Commented Mar 17, 2015 at 12:05
  • possible duplicate of How to repeat div using jQuery or JavaScript? – Sim1 Commented Mar 17, 2015 at 12:15
Add a ment  | 

6 Answers 6

Reset to default 4

Put the elements in the same class using the class attribute, then get the list of all elements using the getElementsByClassName() DOM function. You can then go over the list using a for loop.

[].forEach.call(document.getElementsByClassName("description"), function(elem) {
        elem.innerHTML = "StackOverflow saved my day!";
});

You can even put the text in all elements of the same class using no JavaScript and only CSS by using the content attribute.

First of all, the ID field should be unique per element.

If you give all the tags a class <p class="description"></p> then you can use jQuery to set them all by calling:

$('.description').text('This is the text')

In javascript:

var elements = document.getElementsByClassName("description");
for (var i = 0; i < elements.length; i++) {
    elements[i].innerHTML = "This is the text.";
}

Have a look at the solutions proposed here How to repeat div using jQuery or JavaScript?

this one seems to work pretty well:

html:

<div id="container">data</div>

js:

var container = document.getElementById('container');

function block(mClass, html) {
    //extra html you want to store.
    return '<div class="' + mClass + '">' + html + '</div>';
}

//    code that loops and makes the blocks.
//    first part: creates var i
//    second:     condition, if 'i' is still smaller than three, then loop.
//    third part: increment i by 1;
for (var i = 0; i < 3; i++) {
    // append the result of function 'block()' to the innerHTML
    // of the container.
    container.innerHTML += block('block', 'data');
}

JSFIDDLE

Just added with a code by using

getElementsByClassName()

`<html>
<body>

<div class="example">First div element with class="example".</div>

<p class="example">Second paragraph element with class="example".</p>

<p>Click the button to change the text of the first div element with class="example" (index 0).</p>

<button onclick="myFunction()">Try it</button>

<p><strong>Note:</strong> The getElementsByClassName() method is not supported in Internet Explorer 8 and earlier versions.</p>

<script>
function myFunction() {
    var x = document.getElementsByClassName("example");
    for(var i=0;i< x.length;i++)
    x[i].innerHTML = "Hello World!";
}
</script>

</body>
</html>`

If you wish to keep id, change your code like this :

script :

var pcount = 2// # p
var desc = document.getElementById('description1');
for(i=0; i<pcount;i++){
   document.getElementById('description' + i).innerHTML = desc;
}

html

<div>
 <p id="description1"></p>
</div>
<div>
 <p id="description2"></p>
</div>

two elements cannot have same id but can have same class

 <head>
        <script>
            var x = document.getElementsByClassName("description");
            for (var i = 0; i < x.length; i++) {
                    x[i].innerHTML = "This is the text.";
            }
        </script>

        <style>
               .description1 {                    // this will apply the same style to all elements having class as description1
                  text-align: center;
                  color: red;
               }
        </style>
 </head>

    <body>
        <div>
            <p class="description1"></p>
        </div>
        <div>
            <p class="description1"></p>
        </div>
    </body>

See the script tag. this solves your problem

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744492165a4576911.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信