Remove specific characters from string jquery [duplicate]
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
This question already has an answer here:
jQuery: Add values of checkboxes to input text field
4 answers
OK, So although it seems normal substring or replaces solution answer it is quite complex for me.
I have 3 checkboxes
A,B,C
and one text box
Now, when user select any checkbox the value of checkbox appends into textbox i.e. Fist he selects A -> textbox will be appended by A
Then if selects B -> textbox value will be A, B and so on.
i have done it already.
Now the problem is for unchecking.
If the value of textbox
is A,B,C i.e. the selected checkboxes are A,B,C and if user deselects the B then the value should be A,C and then again deselects the c then the value should be A.
I have tried multiple ways but don't work for all conditions, sometimes it becomes A, C or A, B
Any solution?
Thank you in advance :)
javascript jquery
marked as duplicate by Mohammad, Community♦ Nov 17 '18 at 9:42
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
jQuery: Add values of checkboxes to input text field
4 answers
OK, So although it seems normal substring or replaces solution answer it is quite complex for me.
I have 3 checkboxes
A,B,C
and one text box
Now, when user select any checkbox the value of checkbox appends into textbox i.e. Fist he selects A -> textbox will be appended by A
Then if selects B -> textbox value will be A, B and so on.
i have done it already.
Now the problem is for unchecking.
If the value of textbox
is A,B,C i.e. the selected checkboxes are A,B,C and if user deselects the B then the value should be A,C and then again deselects the c then the value should be A.
I have tried multiple ways but don't work for all conditions, sometimes it becomes A, C or A, B
Any solution?
Thank you in advance :)
javascript jquery
marked as duplicate by Mohammad, Community♦ Nov 17 '18 at 9:42
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
3
Why not just discard the previous value, and calculate the new value from all currently selected boxes?
– CertainPerformance
Nov 17 '18 at 7:30
1
Can you post your working solution for checking the boxes?
– AnonymousSB
Nov 17 '18 at 7:32
add a comment |
This question already has an answer here:
jQuery: Add values of checkboxes to input text field
4 answers
OK, So although it seems normal substring or replaces solution answer it is quite complex for me.
I have 3 checkboxes
A,B,C
and one text box
Now, when user select any checkbox the value of checkbox appends into textbox i.e. Fist he selects A -> textbox will be appended by A
Then if selects B -> textbox value will be A, B and so on.
i have done it already.
Now the problem is for unchecking.
If the value of textbox
is A,B,C i.e. the selected checkboxes are A,B,C and if user deselects the B then the value should be A,C and then again deselects the c then the value should be A.
I have tried multiple ways but don't work for all conditions, sometimes it becomes A, C or A, B
Any solution?
Thank you in advance :)
javascript jquery
This question already has an answer here:
jQuery: Add values of checkboxes to input text field
4 answers
OK, So although it seems normal substring or replaces solution answer it is quite complex for me.
I have 3 checkboxes
A,B,C
and one text box
Now, when user select any checkbox the value of checkbox appends into textbox i.e. Fist he selects A -> textbox will be appended by A
Then if selects B -> textbox value will be A, B and so on.
i have done it already.
Now the problem is for unchecking.
If the value of textbox
is A,B,C i.e. the selected checkboxes are A,B,C and if user deselects the B then the value should be A,C and then again deselects the c then the value should be A.
I have tried multiple ways but don't work for all conditions, sometimes it becomes A, C or A, B
Any solution?
Thank you in advance :)
This question already has an answer here:
jQuery: Add values of checkboxes to input text field
4 answers
javascript jquery
javascript jquery
edited Nov 17 '18 at 8:10
Yashwardhan Pauranik
2,24311731
2,24311731
asked Nov 17 '18 at 7:29
Niket NaikNiket Naik
304
304
marked as duplicate by Mohammad, Community♦ Nov 17 '18 at 9:42
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Mohammad, Community♦ Nov 17 '18 at 9:42
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
3
Why not just discard the previous value, and calculate the new value from all currently selected boxes?
– CertainPerformance
Nov 17 '18 at 7:30
1
Can you post your working solution for checking the boxes?
– AnonymousSB
Nov 17 '18 at 7:32
add a comment |
3
Why not just discard the previous value, and calculate the new value from all currently selected boxes?
– CertainPerformance
Nov 17 '18 at 7:30
1
Can you post your working solution for checking the boxes?
– AnonymousSB
Nov 17 '18 at 7:32
3
3
Why not just discard the previous value, and calculate the new value from all currently selected boxes?
– CertainPerformance
Nov 17 '18 at 7:30
Why not just discard the previous value, and calculate the new value from all currently selected boxes?
– CertainPerformance
Nov 17 '18 at 7:30
1
1
Can you post your working solution for checking the boxes?
– AnonymousSB
Nov 17 '18 at 7:32
Can you post your working solution for checking the boxes?
– AnonymousSB
Nov 17 '18 at 7:32
add a comment |
5 Answers
5
active
oldest
votes
You can simply recreate the list on change of a box
let els = [...document.getElementsByClassName('check')];
els.forEach(e => e.addEventListener('change', () => {
document.getElementById('foo').value = els.filter(x => x.checked).map(x => x.value).join();
}))
.flex {
display: flex;
flex-direction: column;
}
<div class="flex">
<input id="foo" />
<label><input type="checkbox" class="check" value="A"/>A</label>
<label><input type="checkbox" class="check" value="B"/>B</label>
<label><input type="checkbox" class="check" value="C"/>C</label>
</div>
Be aware, this won't work in Internet Explorer, you'll need to convert those ES6 fat arrows and the spread operator. Also, just so you're aware, this solution uses fourfor
loops.
– AnonymousSB
Nov 17 '18 at 8:00
I did check for performance. jsperf.com/compare-foreach-vs-tostring
– AnonymousSB
Nov 17 '18 at 9:36
Btw, I'm not looking to put you on the offensive here. It's easy to forget thatfilter
,map
, andjoin
are all for loops. Even thetoString()
call in my solution is a for loop, because it callsjoin
behind the scenes.
– AnonymousSB
Nov 17 '18 at 9:45
add a comment |
function displayVals() {
var singleValues = $( "#single" ).val();
var multipleValues = $( "#multiple" ).val() || ;
$( "p" ).html( "Single: " + singleValues +
" Multiple: " + multipleValues.join( ", " ) );
}
$( "select" ).change( displayVals );
displayVals();
p {
color: red;
margin: 4px;
}
b {
color: blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p></p>
<select id="single">
<option>Single</option>
<option>Single2</option>
</select>
<select id="multiple" multiple="multiple">
<option selected="selected">A</option>
<option>B</option>
<option selected="selected">C</option>
</select>
add a comment |
Try this:
1) On each checkbox click/unclick get the value of all checked checkboxes. (for example ['A', 'B'])
2) Generate the new string with join the values . ['A', 'B'].join(',') // returns 'A,B'
3) Set the textbox with the new string
If you need code you can submit your current code.
add a comment |
Here's what I came up with, without knowing how you're tracking what's checked or not, it's a data driven approach.
var checked = ;
function updateValue() {
$('#selected').val(checked.toString());
}
$('[type=checkbox]').click(function(e) {
var value = e.target.value;
if(e.target.checked) {
checked.push(value);
} else {
checked.splice(checked.indexOf(value), 1);
}
updateValue();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" value="A">A<br>
<input type="checkbox" value="B">B<br>
<input type="checkbox" value="C">C<br>
<input type="text" id="selected">
add a comment |
$(function() {
const valArray = ; // our value array here what gonna show in textarea
/**
- on change check if checkbox checked or not
- if checked push value of checkbox to array
if not search in our array "valArray" and get it's index then remove it
- change textarea value with the new array "valArray"
*/
$('.checkboxes input').on('change', function(e) {
const $thisVal = $(this).val(),
isChecked = $(this).is(':checked');
if (isChecked) {
valArray.push($thisVal);
} else {
const searchOnThisVal = valArray.find(item => $thisVal === item),
getTheIdx = valArray.indexOf(searchOnThisVal);
valArray.splice(getTheIdx, 1);
}
$('.textarea').empty();
$('.textarea').text(valArray);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="checkboxes">
<label>
<input type="checkbox" value="a">
the checkbox A.
</label>
<label>
<input type="checkbox" value="b">
the checkbox B.
</label>
<label>
<input type="checkbox" value="c">
the checkbox C.
</label>
</div>
<textarea class="textarea" placeholder="your choice..."></textarea>
this should work in your case using jQuery API
add a comment |
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can simply recreate the list on change of a box
let els = [...document.getElementsByClassName('check')];
els.forEach(e => e.addEventListener('change', () => {
document.getElementById('foo').value = els.filter(x => x.checked).map(x => x.value).join();
}))
.flex {
display: flex;
flex-direction: column;
}
<div class="flex">
<input id="foo" />
<label><input type="checkbox" class="check" value="A"/>A</label>
<label><input type="checkbox" class="check" value="B"/>B</label>
<label><input type="checkbox" class="check" value="C"/>C</label>
</div>
Be aware, this won't work in Internet Explorer, you'll need to convert those ES6 fat arrows and the spread operator. Also, just so you're aware, this solution uses fourfor
loops.
– AnonymousSB
Nov 17 '18 at 8:00
I did check for performance. jsperf.com/compare-foreach-vs-tostring
– AnonymousSB
Nov 17 '18 at 9:36
Btw, I'm not looking to put you on the offensive here. It's easy to forget thatfilter
,map
, andjoin
are all for loops. Even thetoString()
call in my solution is a for loop, because it callsjoin
behind the scenes.
– AnonymousSB
Nov 17 '18 at 9:45
add a comment |
You can simply recreate the list on change of a box
let els = [...document.getElementsByClassName('check')];
els.forEach(e => e.addEventListener('change', () => {
document.getElementById('foo').value = els.filter(x => x.checked).map(x => x.value).join();
}))
.flex {
display: flex;
flex-direction: column;
}
<div class="flex">
<input id="foo" />
<label><input type="checkbox" class="check" value="A"/>A</label>
<label><input type="checkbox" class="check" value="B"/>B</label>
<label><input type="checkbox" class="check" value="C"/>C</label>
</div>
Be aware, this won't work in Internet Explorer, you'll need to convert those ES6 fat arrows and the spread operator. Also, just so you're aware, this solution uses fourfor
loops.
– AnonymousSB
Nov 17 '18 at 8:00
I did check for performance. jsperf.com/compare-foreach-vs-tostring
– AnonymousSB
Nov 17 '18 at 9:36
Btw, I'm not looking to put you on the offensive here. It's easy to forget thatfilter
,map
, andjoin
are all for loops. Even thetoString()
call in my solution is a for loop, because it callsjoin
behind the scenes.
– AnonymousSB
Nov 17 '18 at 9:45
add a comment |
You can simply recreate the list on change of a box
let els = [...document.getElementsByClassName('check')];
els.forEach(e => e.addEventListener('change', () => {
document.getElementById('foo').value = els.filter(x => x.checked).map(x => x.value).join();
}))
.flex {
display: flex;
flex-direction: column;
}
<div class="flex">
<input id="foo" />
<label><input type="checkbox" class="check" value="A"/>A</label>
<label><input type="checkbox" class="check" value="B"/>B</label>
<label><input type="checkbox" class="check" value="C"/>C</label>
</div>
You can simply recreate the list on change of a box
let els = [...document.getElementsByClassName('check')];
els.forEach(e => e.addEventListener('change', () => {
document.getElementById('foo').value = els.filter(x => x.checked).map(x => x.value).join();
}))
.flex {
display: flex;
flex-direction: column;
}
<div class="flex">
<input id="foo" />
<label><input type="checkbox" class="check" value="A"/>A</label>
<label><input type="checkbox" class="check" value="B"/>B</label>
<label><input type="checkbox" class="check" value="C"/>C</label>
</div>
let els = [...document.getElementsByClassName('check')];
els.forEach(e => e.addEventListener('change', () => {
document.getElementById('foo').value = els.filter(x => x.checked).map(x => x.value).join();
}))
.flex {
display: flex;
flex-direction: column;
}
<div class="flex">
<input id="foo" />
<label><input type="checkbox" class="check" value="A"/>A</label>
<label><input type="checkbox" class="check" value="B"/>B</label>
<label><input type="checkbox" class="check" value="C"/>C</label>
</div>
let els = [...document.getElementsByClassName('check')];
els.forEach(e => e.addEventListener('change', () => {
document.getElementById('foo').value = els.filter(x => x.checked).map(x => x.value).join();
}))
.flex {
display: flex;
flex-direction: column;
}
<div class="flex">
<input id="foo" />
<label><input type="checkbox" class="check" value="A"/>A</label>
<label><input type="checkbox" class="check" value="B"/>B</label>
<label><input type="checkbox" class="check" value="C"/>C</label>
</div>
answered Nov 17 '18 at 7:40
bambambambam
47.6k877124
47.6k877124
Be aware, this won't work in Internet Explorer, you'll need to convert those ES6 fat arrows and the spread operator. Also, just so you're aware, this solution uses fourfor
loops.
– AnonymousSB
Nov 17 '18 at 8:00
I did check for performance. jsperf.com/compare-foreach-vs-tostring
– AnonymousSB
Nov 17 '18 at 9:36
Btw, I'm not looking to put you on the offensive here. It's easy to forget thatfilter
,map
, andjoin
are all for loops. Even thetoString()
call in my solution is a for loop, because it callsjoin
behind the scenes.
– AnonymousSB
Nov 17 '18 at 9:45
add a comment |
Be aware, this won't work in Internet Explorer, you'll need to convert those ES6 fat arrows and the spread operator. Also, just so you're aware, this solution uses fourfor
loops.
– AnonymousSB
Nov 17 '18 at 8:00
I did check for performance. jsperf.com/compare-foreach-vs-tostring
– AnonymousSB
Nov 17 '18 at 9:36
Btw, I'm not looking to put you on the offensive here. It's easy to forget thatfilter
,map
, andjoin
are all for loops. Even thetoString()
call in my solution is a for loop, because it callsjoin
behind the scenes.
– AnonymousSB
Nov 17 '18 at 9:45
Be aware, this won't work in Internet Explorer, you'll need to convert those ES6 fat arrows and the spread operator. Also, just so you're aware, this solution uses four
for
loops.– AnonymousSB
Nov 17 '18 at 8:00
Be aware, this won't work in Internet Explorer, you'll need to convert those ES6 fat arrows and the spread operator. Also, just so you're aware, this solution uses four
for
loops.– AnonymousSB
Nov 17 '18 at 8:00
I did check for performance. jsperf.com/compare-foreach-vs-tostring
– AnonymousSB
Nov 17 '18 at 9:36
I did check for performance. jsperf.com/compare-foreach-vs-tostring
– AnonymousSB
Nov 17 '18 at 9:36
Btw, I'm not looking to put you on the offensive here. It's easy to forget that
filter
, map
, and join
are all for loops. Even the toString()
call in my solution is a for loop, because it calls join
behind the scenes.– AnonymousSB
Nov 17 '18 at 9:45
Btw, I'm not looking to put you on the offensive here. It's easy to forget that
filter
, map
, and join
are all for loops. Even the toString()
call in my solution is a for loop, because it calls join
behind the scenes.– AnonymousSB
Nov 17 '18 at 9:45
add a comment |
function displayVals() {
var singleValues = $( "#single" ).val();
var multipleValues = $( "#multiple" ).val() || ;
$( "p" ).html( "Single: " + singleValues +
" Multiple: " + multipleValues.join( ", " ) );
}
$( "select" ).change( displayVals );
displayVals();
p {
color: red;
margin: 4px;
}
b {
color: blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p></p>
<select id="single">
<option>Single</option>
<option>Single2</option>
</select>
<select id="multiple" multiple="multiple">
<option selected="selected">A</option>
<option>B</option>
<option selected="selected">C</option>
</select>
add a comment |
function displayVals() {
var singleValues = $( "#single" ).val();
var multipleValues = $( "#multiple" ).val() || ;
$( "p" ).html( "Single: " + singleValues +
" Multiple: " + multipleValues.join( ", " ) );
}
$( "select" ).change( displayVals );
displayVals();
p {
color: red;
margin: 4px;
}
b {
color: blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p></p>
<select id="single">
<option>Single</option>
<option>Single2</option>
</select>
<select id="multiple" multiple="multiple">
<option selected="selected">A</option>
<option>B</option>
<option selected="selected">C</option>
</select>
add a comment |
function displayVals() {
var singleValues = $( "#single" ).val();
var multipleValues = $( "#multiple" ).val() || ;
$( "p" ).html( "Single: " + singleValues +
" Multiple: " + multipleValues.join( ", " ) );
}
$( "select" ).change( displayVals );
displayVals();
p {
color: red;
margin: 4px;
}
b {
color: blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p></p>
<select id="single">
<option>Single</option>
<option>Single2</option>
</select>
<select id="multiple" multiple="multiple">
<option selected="selected">A</option>
<option>B</option>
<option selected="selected">C</option>
</select>
function displayVals() {
var singleValues = $( "#single" ).val();
var multipleValues = $( "#multiple" ).val() || ;
$( "p" ).html( "Single: " + singleValues +
" Multiple: " + multipleValues.join( ", " ) );
}
$( "select" ).change( displayVals );
displayVals();
p {
color: red;
margin: 4px;
}
b {
color: blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p></p>
<select id="single">
<option>Single</option>
<option>Single2</option>
</select>
<select id="multiple" multiple="multiple">
<option selected="selected">A</option>
<option>B</option>
<option selected="selected">C</option>
</select>
function displayVals() {
var singleValues = $( "#single" ).val();
var multipleValues = $( "#multiple" ).val() || ;
$( "p" ).html( "Single: " + singleValues +
" Multiple: " + multipleValues.join( ", " ) );
}
$( "select" ).change( displayVals );
displayVals();
p {
color: red;
margin: 4px;
}
b {
color: blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p></p>
<select id="single">
<option>Single</option>
<option>Single2</option>
</select>
<select id="multiple" multiple="multiple">
<option selected="selected">A</option>
<option>B</option>
<option selected="selected">C</option>
</select>
function displayVals() {
var singleValues = $( "#single" ).val();
var multipleValues = $( "#multiple" ).val() || ;
$( "p" ).html( "Single: " + singleValues +
" Multiple: " + multipleValues.join( ", " ) );
}
$( "select" ).change( displayVals );
displayVals();
p {
color: red;
margin: 4px;
}
b {
color: blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p></p>
<select id="single">
<option>Single</option>
<option>Single2</option>
</select>
<select id="multiple" multiple="multiple">
<option selected="selected">A</option>
<option>B</option>
<option selected="selected">C</option>
</select>
answered Nov 17 '18 at 8:38
Istiyak AminIstiyak Amin
15313
15313
add a comment |
add a comment |
Try this:
1) On each checkbox click/unclick get the value of all checked checkboxes. (for example ['A', 'B'])
2) Generate the new string with join the values . ['A', 'B'].join(',') // returns 'A,B'
3) Set the textbox with the new string
If you need code you can submit your current code.
add a comment |
Try this:
1) On each checkbox click/unclick get the value of all checked checkboxes. (for example ['A', 'B'])
2) Generate the new string with join the values . ['A', 'B'].join(',') // returns 'A,B'
3) Set the textbox with the new string
If you need code you can submit your current code.
add a comment |
Try this:
1) On each checkbox click/unclick get the value of all checked checkboxes. (for example ['A', 'B'])
2) Generate the new string with join the values . ['A', 'B'].join(',') // returns 'A,B'
3) Set the textbox with the new string
If you need code you can submit your current code.
Try this:
1) On each checkbox click/unclick get the value of all checked checkboxes. (for example ['A', 'B'])
2) Generate the new string with join the values . ['A', 'B'].join(',') // returns 'A,B'
3) Set the textbox with the new string
If you need code you can submit your current code.
answered Nov 17 '18 at 7:38
user2693928user2693928
1,9211716
1,9211716
add a comment |
add a comment |
Here's what I came up with, without knowing how you're tracking what's checked or not, it's a data driven approach.
var checked = ;
function updateValue() {
$('#selected').val(checked.toString());
}
$('[type=checkbox]').click(function(e) {
var value = e.target.value;
if(e.target.checked) {
checked.push(value);
} else {
checked.splice(checked.indexOf(value), 1);
}
updateValue();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" value="A">A<br>
<input type="checkbox" value="B">B<br>
<input type="checkbox" value="C">C<br>
<input type="text" id="selected">
add a comment |
Here's what I came up with, without knowing how you're tracking what's checked or not, it's a data driven approach.
var checked = ;
function updateValue() {
$('#selected').val(checked.toString());
}
$('[type=checkbox]').click(function(e) {
var value = e.target.value;
if(e.target.checked) {
checked.push(value);
} else {
checked.splice(checked.indexOf(value), 1);
}
updateValue();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" value="A">A<br>
<input type="checkbox" value="B">B<br>
<input type="checkbox" value="C">C<br>
<input type="text" id="selected">
add a comment |
Here's what I came up with, without knowing how you're tracking what's checked or not, it's a data driven approach.
var checked = ;
function updateValue() {
$('#selected').val(checked.toString());
}
$('[type=checkbox]').click(function(e) {
var value = e.target.value;
if(e.target.checked) {
checked.push(value);
} else {
checked.splice(checked.indexOf(value), 1);
}
updateValue();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" value="A">A<br>
<input type="checkbox" value="B">B<br>
<input type="checkbox" value="C">C<br>
<input type="text" id="selected">
Here's what I came up with, without knowing how you're tracking what's checked or not, it's a data driven approach.
var checked = ;
function updateValue() {
$('#selected').val(checked.toString());
}
$('[type=checkbox]').click(function(e) {
var value = e.target.value;
if(e.target.checked) {
checked.push(value);
} else {
checked.splice(checked.indexOf(value), 1);
}
updateValue();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" value="A">A<br>
<input type="checkbox" value="B">B<br>
<input type="checkbox" value="C">C<br>
<input type="text" id="selected">
var checked = ;
function updateValue() {
$('#selected').val(checked.toString());
}
$('[type=checkbox]').click(function(e) {
var value = e.target.value;
if(e.target.checked) {
checked.push(value);
} else {
checked.splice(checked.indexOf(value), 1);
}
updateValue();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" value="A">A<br>
<input type="checkbox" value="B">B<br>
<input type="checkbox" value="C">C<br>
<input type="text" id="selected">
var checked = ;
function updateValue() {
$('#selected').val(checked.toString());
}
$('[type=checkbox]').click(function(e) {
var value = e.target.value;
if(e.target.checked) {
checked.push(value);
} else {
checked.splice(checked.indexOf(value), 1);
}
updateValue();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" value="A">A<br>
<input type="checkbox" value="B">B<br>
<input type="checkbox" value="C">C<br>
<input type="text" id="selected">
edited Nov 17 '18 at 7:58
answered Nov 17 '18 at 7:51
AnonymousSBAnonymousSB
2,239221
2,239221
add a comment |
add a comment |
$(function() {
const valArray = ; // our value array here what gonna show in textarea
/**
- on change check if checkbox checked or not
- if checked push value of checkbox to array
if not search in our array "valArray" and get it's index then remove it
- change textarea value with the new array "valArray"
*/
$('.checkboxes input').on('change', function(e) {
const $thisVal = $(this).val(),
isChecked = $(this).is(':checked');
if (isChecked) {
valArray.push($thisVal);
} else {
const searchOnThisVal = valArray.find(item => $thisVal === item),
getTheIdx = valArray.indexOf(searchOnThisVal);
valArray.splice(getTheIdx, 1);
}
$('.textarea').empty();
$('.textarea').text(valArray);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="checkboxes">
<label>
<input type="checkbox" value="a">
the checkbox A.
</label>
<label>
<input type="checkbox" value="b">
the checkbox B.
</label>
<label>
<input type="checkbox" value="c">
the checkbox C.
</label>
</div>
<textarea class="textarea" placeholder="your choice..."></textarea>
this should work in your case using jQuery API
add a comment |
$(function() {
const valArray = ; // our value array here what gonna show in textarea
/**
- on change check if checkbox checked or not
- if checked push value of checkbox to array
if not search in our array "valArray" and get it's index then remove it
- change textarea value with the new array "valArray"
*/
$('.checkboxes input').on('change', function(e) {
const $thisVal = $(this).val(),
isChecked = $(this).is(':checked');
if (isChecked) {
valArray.push($thisVal);
} else {
const searchOnThisVal = valArray.find(item => $thisVal === item),
getTheIdx = valArray.indexOf(searchOnThisVal);
valArray.splice(getTheIdx, 1);
}
$('.textarea').empty();
$('.textarea').text(valArray);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="checkboxes">
<label>
<input type="checkbox" value="a">
the checkbox A.
</label>
<label>
<input type="checkbox" value="b">
the checkbox B.
</label>
<label>
<input type="checkbox" value="c">
the checkbox C.
</label>
</div>
<textarea class="textarea" placeholder="your choice..."></textarea>
this should work in your case using jQuery API
add a comment |
$(function() {
const valArray = ; // our value array here what gonna show in textarea
/**
- on change check if checkbox checked or not
- if checked push value of checkbox to array
if not search in our array "valArray" and get it's index then remove it
- change textarea value with the new array "valArray"
*/
$('.checkboxes input').on('change', function(e) {
const $thisVal = $(this).val(),
isChecked = $(this).is(':checked');
if (isChecked) {
valArray.push($thisVal);
} else {
const searchOnThisVal = valArray.find(item => $thisVal === item),
getTheIdx = valArray.indexOf(searchOnThisVal);
valArray.splice(getTheIdx, 1);
}
$('.textarea').empty();
$('.textarea').text(valArray);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="checkboxes">
<label>
<input type="checkbox" value="a">
the checkbox A.
</label>
<label>
<input type="checkbox" value="b">
the checkbox B.
</label>
<label>
<input type="checkbox" value="c">
the checkbox C.
</label>
</div>
<textarea class="textarea" placeholder="your choice..."></textarea>
this should work in your case using jQuery API
$(function() {
const valArray = ; // our value array here what gonna show in textarea
/**
- on change check if checkbox checked or not
- if checked push value of checkbox to array
if not search in our array "valArray" and get it's index then remove it
- change textarea value with the new array "valArray"
*/
$('.checkboxes input').on('change', function(e) {
const $thisVal = $(this).val(),
isChecked = $(this).is(':checked');
if (isChecked) {
valArray.push($thisVal);
} else {
const searchOnThisVal = valArray.find(item => $thisVal === item),
getTheIdx = valArray.indexOf(searchOnThisVal);
valArray.splice(getTheIdx, 1);
}
$('.textarea').empty();
$('.textarea').text(valArray);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="checkboxes">
<label>
<input type="checkbox" value="a">
the checkbox A.
</label>
<label>
<input type="checkbox" value="b">
the checkbox B.
</label>
<label>
<input type="checkbox" value="c">
the checkbox C.
</label>
</div>
<textarea class="textarea" placeholder="your choice..."></textarea>
this should work in your case using jQuery API
$(function() {
const valArray = ; // our value array here what gonna show in textarea
/**
- on change check if checkbox checked or not
- if checked push value of checkbox to array
if not search in our array "valArray" and get it's index then remove it
- change textarea value with the new array "valArray"
*/
$('.checkboxes input').on('change', function(e) {
const $thisVal = $(this).val(),
isChecked = $(this).is(':checked');
if (isChecked) {
valArray.push($thisVal);
} else {
const searchOnThisVal = valArray.find(item => $thisVal === item),
getTheIdx = valArray.indexOf(searchOnThisVal);
valArray.splice(getTheIdx, 1);
}
$('.textarea').empty();
$('.textarea').text(valArray);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="checkboxes">
<label>
<input type="checkbox" value="a">
the checkbox A.
</label>
<label>
<input type="checkbox" value="b">
the checkbox B.
</label>
<label>
<input type="checkbox" value="c">
the checkbox C.
</label>
</div>
<textarea class="textarea" placeholder="your choice..."></textarea>
$(function() {
const valArray = ; // our value array here what gonna show in textarea
/**
- on change check if checkbox checked or not
- if checked push value of checkbox to array
if not search in our array "valArray" and get it's index then remove it
- change textarea value with the new array "valArray"
*/
$('.checkboxes input').on('change', function(e) {
const $thisVal = $(this).val(),
isChecked = $(this).is(':checked');
if (isChecked) {
valArray.push($thisVal);
} else {
const searchOnThisVal = valArray.find(item => $thisVal === item),
getTheIdx = valArray.indexOf(searchOnThisVal);
valArray.splice(getTheIdx, 1);
}
$('.textarea').empty();
$('.textarea').text(valArray);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="checkboxes">
<label>
<input type="checkbox" value="a">
the checkbox A.
</label>
<label>
<input type="checkbox" value="b">
the checkbox B.
</label>
<label>
<input type="checkbox" value="c">
the checkbox C.
</label>
</div>
<textarea class="textarea" placeholder="your choice..."></textarea>
edited Nov 17 '18 at 8:57
answered Nov 17 '18 at 8:38
Amir FawzyAmir Fawzy
25139
25139
add a comment |
add a comment |
3
Why not just discard the previous value, and calculate the new value from all currently selected boxes?
– CertainPerformance
Nov 17 '18 at 7:30
1
Can you post your working solution for checking the boxes?
– AnonymousSB
Nov 17 '18 at 7:32