简体   繁体   中英

add variable on array javascript

i have a javascript code for change automatic placeholder from change keyword. This is the smallest part of the code :

 $("#addrow").on("click", function () {

            counter = $('#myTable tr').length -1;

        var newRow = $("<tr>");
        var cols = "";
        ....
        cols += '<td><input type="text" name="keyword' + counter + '" placeholder="add keyword in here.." style="width: 425px;"/></td>';

        cols += '<td><input type="button" class="ibtnDel"  value="-"></td>';
        newRow.append(cols);
        //if (counter == 4) $('#addrow').attr('disabled', true).prop('value', "You've reached the limit");
        $("table.order-list").append(newRow);

        counter++;

        $("#list_field"+counter).change(function(){
    if ($(this).val() == 'all'){
        $("input[name=keyword]").attr('placeholder', 'add keyword in here');
    }
    else if ($(this).val() == 'chrom'){
        $("input[name=keyword]").attr('placeholder', 'ex: 8');
    } });


});

that was the problem for me is how to add the variable "counter" in "$("input[name=keyword]").attr('placeholder', 'add keyword in here');" . is like this? ->

$("input[name=keyword]").counter.attr('placeholder', 'add keyword in here');

sorry I am still learning about javascript. thanks for your solution.

if you want add a variable in text of placeholder here is the code

var counter =1 ; 
$("input[name=keyword]").attr('placeholder', 'add keyword in here '+counter);

if you want discriminate each input you can add an id

var counter =1 ; 
$("input[name=keyword]").attr('id', counter);

also you can find an element with css selector nth-child(number-of-item)

html

<input class="input" palceholder="blabla1">
<input class="input" palceholder="blabla2">
<input class="input" palceholder="blabla3">

javascript

var counter =1 ; 
$(".inputs:nth-child("+counter+")")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM