Jump to content

loop input for each string


Mike Hemeles

Recommended Posts

Hello,
I want to set of my input text value of a string where I split in advance, so for each array of the string set it of input!
I should it like this but the input don't loop.
 

$(function () {
    $('#submit').click(function () {
        var met = $('#nom').val();
        var metiers = met.split(', ');
        var join = "";
        $.each(metiers, function(index, value) {
            join += value;
            $('#input').each(function () {
                $(this).val(join);
            });
        });

    })
});

who can I do so that my input split and have all value of string exploded, thanks

Link to comment
Share on other sites

That inner each shouldn't be there, you're trying to loop over everything that has the ID "input" but you should only have 1 element with a certain ID.  I'm not sure what you're really trying to do though, you split something up on commas (followed by a space), then loop through those values and add each one to a string.  So all you're doing is removing the commas and following spaces.

Link to comment
Share on other sites

thanks, I have tried to do your indications like this, 

var count = 1;
$(function () {
    $('#submit').click(function () {
        var met = $('#nom').val();
        var metiers = met.split(', ');
        var join = "";
        $.each(metiers, function(index, valeur) {
            join += valeur + "<br>";

            var element = document.createElement("input");

            element.setAttribute("type", "text");
            element.setAttribute("value", valeur);
            element.setAttribute("name", "txtbox"+ count);

            var vals = document.getElementById("inputbox");

            vals.appendChild(element);
            var br = document.createElement("br");
            vals.appendChild(br);

        });


    })
});
<div id = "inputbox">
    <input type="text" value="A, B, C, D" id="nom">
</div>

I don't know where is wrong but the input doesn't appears :mellow::mellow:

Link to comment
Share on other sites

You don't need join, you can just use the parameter variable from function directly. You need to clear any of these created inputs first before creating new inputs, ignoring the user input text box, either by class name or name attribute that begins with specific string name.

Link to comment
Share on other sites

When you click submit button, the first thing it should do to prevent duplication of created inputs IS remove any existing created inputs that may exist already, using jquery each loop again looking for attribute 'name' whose value begins with 'txtbox'.

Example for selector is here

https://api.jquery.com/attribute-equals-selector/

Here describes characters you can use to filter out specific inputs

https://www.w3schools.com/css/css_attribute_selectors.asp

Then use remove() to remove these elements that match

https://api.jquery.com/remove/

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...