Hi guys,

I want to assign something into label via JS but code below doesn't work for array. Do I miss something?

Thanks in advance

<script>
function calculate_cost(whichlabel){
  document.getElementById('text_cost['+whichlabel+']').innerHTML = 'show me in label below';
}


<input type="text" name="text_qty[]" value="0" size="3" maxlength="5" style="text-align:center;" onkeyup="calculate_cost(0)" />

<label id="text_cost[]">0.00</label>

did you try putting a 0 in the label id?

label id="text_cost[0]">0.00</label>

Maybe something like this. You are using arrays, so you have to define them in your function.

function motor(){
    num = document.getElementyById("input").value;
    var qnt = new Array();
    qnt[0] = num;
    var n = qnt.length;
    var show = new Array();
    for(i=0;i<n;i++){
        show[i] = getElementTagName("label");
        show[i].innerHTML = qnt[i];
    }
}
<a onclick="motor()" id="input">

UNTESTED!!

I'll try it on Monday. Thanks

OK. Putting 0 in it solved problem. Thanks guys

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.