JavaScript how to send different objects but keeping the same id?

Suppose I have two areas separated by div containers, I need to send Id values ​​from different areas, locally, when a function is called in that container.

extract:

   <script> 
       document.getElementById('compare_name").innerHTML = "Changed";
</script>

<div class = "details>

    <span id="compare_name">no1</span> 
    <form id="view-details0">
    <input type="button" value="Add to Compare" onclick="add_compare()">
    </form>
</div>

<div class  = "details">

<span id="compare_name">no2</span> 
    <form id="view-details0">
    <input type="button" value="Add to Compare" onclick="add_compare()">
    </form>
</div>

      

+3


source to share


3 answers


You cannot use the same ID for multiple items. you can use the name as below:



<script> 
       var x = document.getElementsByName("compare_name");
for(var i = 0; i < x.length ; i++){
x[i].innerHTML = "Changed";
                        }
</script>

<div class = "details>

    <span id="compare_name" name="compare_name">no1</span> 
    <form id="view-details0">
    <input type="button" value="Add to Compare" onclick="add_compare()">
    </form>
</div>

<div class  = "details">

<span id="compare_name" name="compare_name">no2</span> 
    <form id="view-details0">
    <input type="button" value="Add to Compare" onclick="add_compare()">
    </form>
</div>

      

+2


source


As others show, to follow the rule, the ID attribute should not be duplicated, but it doesn't matter if you don't rely on it.

My guess is that the examiner might only need to change one text inside the div that is clicked, not all of the gaps, so it just changes a little.



<html>
<head>
<title>Test</title>
<script>
function add_compare(el) {
    var divChildEles = el.parentNode.parentNode.childNodes;
    for (var i=0; i<divChildEles.length; i++) {
        if (divChildEles[i].nodeType==1 && divChildEles[i].nodeName=="SPAN") {
            //console.log(divChildEles[i].innerText);
            divChildEles[i].innerText='changed';
        }
    }
}

</script>
</head>

<body>
<div class = "details">
    <span id="compare_name">no1</span> 
    <form id="view-details0">
    <input type="button" value="Add to Compare" onclick="add_compare(this)">
    </form>
</div>

<div class  = "details">
    <span id="compare_name">no2</span> 
    <form id="view-details1">
    <input type="button" value="Add to Compare" onclick="add_compare(this)">
    </form>
</div>
</body>
</html>

      

+1


source


id in JavaScript must be unique. instead you can give them a class with the same name

<script> 
       var x=document.getElementByClassName('compare_name");
        for(var i=0;i<x.lengh;i++)
           x[i].innerHTML = "Changed"
</script>

<div class = "details>

    <span class="compare_name">no1</span> 
    <form id="view-details0">
    <input type="button" value="Add to Compare" onclick="add_compare()">
    </form>
</div>

<div class  = "details">

<span class="compare_name">no2</span> 
    <form id="view-details0">
    <input type="button" value="Add to Compare" onclick="add_compare()">
    </form>
</div>

      

-1


source







All Articles