简体   繁体   中英

I create dynamically some text area in a div. How can i get its names in javascript?

This is my code to create the textarea and it works fine, but I want to know how many textarea the user creates and their names.

function createBoxEquip() {

    $codEquip = $('#equipamento').val();
    $nomeEquip = $('#equipamento>option:selected').text();
    $novadiv = "#div"+$codEquip;

    if ( !$( $novadiv ).length ) {
        $("#equip_tot").append('<div class="box"name=div'+$codEquip+'id=div'+$codEquip+'></div>')
        $("#div"+$codEquip).append('<span class="titulo1" name='+$codEquip+' id='+$codEquip+'> - '+$nomeEquip+'</span><span name=texto'+$codEquip+' id=texto'+$codEquip+'><br>&nbsp;</span>');
        $("#div"+$codEquip).append('<input type="button" name=apagar'+$codEquip+' id=apagar'+$codEquip+' value="Remover" onclick="deleteBoxEquip('+$codEquip+')"><span name=texto1'+$codEquip+' id=texto1'+$codEquip+'>&nbsp;&nbsp;&nbsp;<br></span>');
        $("#div"+$codEquip).append('<input type="text" style="width: 20px;" name=contalinhas'+$codEquip+' id=contalinhas'+$codEquip+'><span name=texto2'+$codEquip+' id=texto2'+$codEquip+'><br></span>');*/
        $("#div"+$codEquip).append('<textarea style="width: 150px;" rows=12 name=numerosserie'+$codEquip+' id=numerosserie'+$codEquip+' value="'+$codEquip+' - '+$nomeEquip+'"/><span name=texto3'+$codEquip+' id=texto3'+$codEquip+'>&nbsp;</span>');
    }
}

As long as you can pre-determine what the names of the textarea will be, for example - I've written similar code that generated a bunch of <div> tags with unique ID's, each Id was numeric, so I'd auto-generate a bunch of tags like this:

<div id="div-0">Zero</div>
<div id="div-1">One</div>
<div id="div-2">Two</div>

Because I know in advance that each div id will have the prefix div- followed by a digit which begins at 0 and increments sequentially, I can iterate through each element in a loop, and know when I've reached an undefined element:

function loopElements() {
    var divPrefix = "div-";
    var divNo = 0;

    // Loop through all div- tags:
    //
    while (true) {
        // The .length property will return 0 if the element
        // doesn't exist...
        //
        if ($("#" + divPrefix + divNo.toString()).length == 0)
            // This div doesn't exist, bail!
            //
            break;

        // do something with div

        divNo++;
    }
}

Something like this would work, it depends on the names/id's you're creating, and if you can somehow predetermine what they should be.

hope this helps.

EDIT:

Having read your question again I think the above solution may not be what you're looking for, if not I apologise.

There are some ambiguities with your question...exactly how are these names created? Does the user choose them? Are they generated programmatically?

You should post more code and explain in greater detail.

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