Reputation: 1192
I have a form
that allow the user to modify a file, and I'm having some troubles to replace in the file some values. I don't know how I should do the loop and replace in the object.
I need to keep the index of both like this:
obj.environment[0] = children[0]
obj.environment[1] = children[1]
and as you can see, maybe there are more environment
in the form than in the object
because the user added some, so it should add them after.
myObj = {environment: ["a","b","c"]}
$('#labelEnv').children('input').each(function () {
myObj.environment[id] = this.value
});
.form-style-2{
max-width: 600px;
padding: 10px 10px 2px 10px;
font: 13px Arial, Helvetica, sans-serif;
background: rgba(blue, 0.8);
}
.form-style-2-heading{
color:black;
font-weight: bold;
font-style: italic;
border-bottom: 2px solid #ddd;
margin-bottom: 20px;
font-size: 15px;
padding-bottom: 3px;
}
.form-style-2 label{
display:table;
width: 100%;
font-size: 15px;
}
.form-style-2 label > span{
color: black;
font-weight: bold;
padding-right: 5px;
width:25%;
display: table-cell;
}
.form-style-2 span.required{
color:red;
}
.form-style-2 a{
display: block;
}
.form-style-2 input.input-field {
border: 1px solid #c2c2c2;
border-radius: 3px;
box-sizing: border-box;
display: table-cell;
margin: 4px;
outline: medium none;
padding: 7px;
width: 31%;
}
.form-style-2 input.env, input.vol{
width: 100% !important;
}
.form-style-2 input.nameModif{
width: 50% !important;
}
.form-style-2 .input-field:focus{
border: 1px solid #0C0;
}
.form-style-2 input.saveModif{
border: none;
padding: 5px 5px 5px 5px;
background: green;
color: #fff;
box-shadow: 1px 1px 4px #DADADA;
border-radius: 3px;
margin-right: 10px;
}
.form-style-2 input.saveModif:hover{
background: green;
color: #fff;
}
.form-style-2 input.cancelModif{
border: none;
padding: 5px 5px 5px 5px;
background: red;
color: #fff;
box-shadow: 1px 1px 4px #DADADA;
border-radius: 3px;
}
.form-style-2 input.cancelModif:hover{
background: red;
color: #fff;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="divCodeContainer">
<div class="form-style-2">
<div class="form-style-2-heading"></div>
<form action="" method="post">
<label>
<span>Container name
<span class="required">*</span>
</span>
<input type="text" class="input-field nameModif" value="" />
</label>
<label id="labelEnv">
<span>Environment
</span>
<input type="text" class="input-field env" value="replace me" />
<input type="text" class="input-field env" value="replace me" />
<input type="text" class="input-field env" value="replace me" />
<input type="text" class="input-field env" value="replace me" />
<a class="addEnv" aria-expanded="false"><i class="fa fa-plus"></i></a>
</label>
<label>
<span> </span>
<input class="saveModif" type="button" value="Save" />
<input class="cancelModif" type="button" value="Cancel" />
</label>
</form>
</div>
</div>
the snippet is not working and it's wrong because I don't know how I should make the loop
Upvotes: 0
Views: 33
Reputation: 27051
Add index
to your .each()
like .each(function (index)
Then you can use myObj.environment[index]
$('#labelEnv').children('input').each(function (index) {
myObj.environment[index] = this.value
});
myObj = {environment: ["a","b","c"]}
$('#labelEnv').children('input').each(function (index) {
myObj.environment[index] = this.value
});
console.log(myObj)
.form-style-2{
max-width: 600px;
padding: 10px 10px 2px 10px;
font: 13px Arial, Helvetica, sans-serif;
background: rgba(blue, 0.8);
}
.form-style-2-heading{
color:black;
font-weight: bold;
font-style: italic;
border-bottom: 2px solid #ddd;
margin-bottom: 20px;
font-size: 15px;
padding-bottom: 3px;
}
.form-style-2 label{
display:table;
width: 100%;
font-size: 15px;
}
.form-style-2 label > span{
color: black;
font-weight: bold;
padding-right: 5px;
width:25%;
display: table-cell;
}
.form-style-2 span.required{
color:red;
}
.form-style-2 a{
display: block;
}
.form-style-2 input.input-field {
border: 1px solid #c2c2c2;
border-radius: 3px;
box-sizing: border-box;
display: table-cell;
margin: 4px;
outline: medium none;
padding: 7px;
width: 31%;
}
.form-style-2 input.env, input.vol{
width: 100% !important;
}
.form-style-2 input.nameModif{
width: 50% !important;
}
.form-style-2 .input-field:focus{
border: 1px solid #0C0;
}
.form-style-2 input.saveModif{
border: none;
padding: 5px 5px 5px 5px;
background: green;
color: #fff;
box-shadow: 1px 1px 4px #DADADA;
border-radius: 3px;
margin-right: 10px;
}
.form-style-2 input.saveModif:hover{
background: green;
color: #fff;
}
.form-style-2 input.cancelModif{
border: none;
padding: 5px 5px 5px 5px;
background: red;
color: #fff;
box-shadow: 1px 1px 4px #DADADA;
border-radius: 3px;
}
.form-style-2 input.cancelModif:hover{
background: red;
color: #fff;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="divCodeContainer">
<div class="form-style-2">
<div class="form-style-2-heading"></div>
<form action="" method="post">
<label>
<span>Container name
<span class="required">*</span>
</span>
<input type="text" class="input-field nameModif" value="" />
</label>
<label id="labelEnv">
<span>Environment
</span>
<input type="text" class="input-field env" value="replace me" />
<input type="text" class="input-field env" value="replace me" />
<input type="text" class="input-field env" value="replace me" />
<input type="text" class="input-field env" value="replace me" />
<a class="addEnv" aria-expanded="false"><i class="fa fa-plus"></i></a>
</label>
<label>
<span> </span>
<input class="saveModif" type="button" value="Save" />
<input class="cancelModif" type="button" value="Cancel" />
</label>
</form>
</div>
</div>
Upvotes: 1
Reputation: 133453
Use index of $.each()
$('#labelEnv').children('input').each(function(index) {
myObj.environment[index] = this.value
});
myObj = {
environment: ["a", "b", "c"]
}
$('#labelEnv').children('input').each(function(index) {
myObj.environment[index] = this.value
});
console.log(myObj.environment);
.form-style-2 {
max-width: 600px;
padding: 10px 10px 2px 10px;
font: 13px Arial, Helvetica, sans-serif;
background: rgba(blue, 0.8);
}
.form-style-2-heading {
color: black;
font-weight: bold;
font-style: italic;
border-bottom: 2px solid #ddd;
margin-bottom: 20px;
font-size: 15px;
padding-bottom: 3px;
}
.form-style-2 label {
display: table;
width: 100%;
font-size: 15px;
}
.form-style-2 label>span {
color: black;
font-weight: bold;
padding-right: 5px;
width: 25%;
display: table-cell;
}
.form-style-2 span.required {
color: red;
}
.form-style-2 a {
display: block;
}
.form-style-2 input.input-field {
border: 1px solid #c2c2c2;
border-radius: 3px;
box-sizing: border-box;
display: table-cell;
margin: 4px;
outline: medium none;
padding: 7px;
width: 31%;
}
.form-style-2 input.env,
input.vol {
width: 100% !important;
}
.form-style-2 input.nameModif {
width: 50% !important;
}
.form-style-2 .input-field:focus {
border: 1px solid #0C0;
}
.form-style-2 input.saveModif {
border: none;
padding: 5px 5px 5px 5px;
background: green;
color: #fff;
box-shadow: 1px 1px 4px #DADADA;
border-radius: 3px;
margin-right: 10px;
}
.form-style-2 input.saveModif:hover {
background: green;
color: #fff;
}
.form-style-2 input.cancelModif {
border: none;
padding: 5px 5px 5px 5px;
background: red;
color: #fff;
box-shadow: 1px 1px 4px #DADADA;
border-radius: 3px;
}
.form-style-2 input.cancelModif:hover {
background: red;
color: #fff;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="divCodeContainer">
<div class="form-style-2">
<div class="form-style-2-heading"></div>
<form action="" method="post">
<label>
<span>Container name
<span class="required">*</span>
</span>
<input type="text" class="input-field nameModif" value="" />
</label>
<label id="labelEnv">
<span>Environment
</span>
<input type="text" class="input-field env" value="replace me a" />
<input type="text" class="input-field env" value="replace me b" />
<input type="text" class="input-field env" value="replace me c" />
<input type="text" class="input-field env" value="replace me" />
<a class="addEnv" aria-expanded="false"><i class="fa fa-plus"></i></a>
</label>
<label>
<span> </span>
<input class="saveModif" type="button" value="Save" />
<input class="cancelModif" type="button" value="Cancel" />
</label>
</form>
</div>
</div>
Upvotes: 1
Reputation: 1964
How about:
myObj.environment = $('#labelEnv').children('input').get().map( e => e.value )
Upvotes: 1