- 전체
- HTML
- Web Design (웹디자인)
- XE 응용 개발
- wordpress plugin dev
- Javascript & JavaScript Application
- MEAN Stack : full stack javascript
- angular js & ionic framework
- bootstrap
- WebGL, Three.js and Babylon.js
- restful api design
- mobile web
- node.js 응용
- Cloud Service 응용
- 웹 어셈블리 개발 [WASM, WebAssembly]
- 마이크로서비스, MSA (microservice architecture)
- WebGL / WebGPU
- next.js 개발
- micro frontend (마이크로프론트앤드)
- 전자상거래/쇼핑몰
- 서버 클라우드 (aws, azure, google)
JQuery [HTML5, Javascript, Jquery] Add item from array to list when button is clicked in jQuery
2021.06.01 17:39
[HTML5, Javascript, Jquery] Add item from array to list when button is clicked in jQuery
[Qustion]
I'm building a UI where a user can select from a list, include the selection in an array and then display the array in another list. I'm very close. However, every time I add an item to the list it repeats items.
How do I clear the list before it builds from the array again.
You can see how it works and my code here:
$(document).ready(function(){
var a = [];
$(".btn").click(function(){
a.push($(this).next("span").text());
$(this).closest("li").hide();
$( "#s" ).text( a.join( " " ) );
$.each(a, function(i){
var cList = $('ul.mylist');
var li = $('<li/>')
.addClass('ui-menu-item')
.attr('role', 'menuitem')
.appendTo(cList);
var aaa = $('<a/>')
.addClass('ui-all')
.text(a[i])
.appendTo(li);
});
});
});
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Selector</title>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<h2>List of Options</h2>
<ul>
<li><button class="btn">Select</button><span>One</span></li>
<li><button class="btn">Select</button><span>Two</span></li>
<li><button class="btn">Select</button><span>Three</span></li>
<li><button class="btn">Select</button><span>Four</span></li>
</ul>
<h2>List of Selections</h2>
<ul class="mylist">
</ul>
<h2>Array</h2>
<div>Array of Selected Items to Send to DB:</div>
<span ID="s"></span>
</body>
</html>
[Answer]
You have to empty the Ul html first because you make a each function which append all values of array again and again so it repeat the values. You can see live demo here :-
$(document).ready(function(){
var a = [];
$(".btn").click(function(){
var obj = $(this);
a.push(obj.next("span").text());
console.log(a);
obj.parent().hide();
$( "#s" ).text( a.join( " " ) );
$('ul.mylist').html("");
$.each(a, function(i){
var cList = $('ul.mylist');
var li = $('<li/>')
.addClass('ui-menu-item')
.attr('role', 'menuitem')
.appendTo(cList);
var aaa = $('<a/>')
.addClass('ui-all')
.text(a[i])
.appendTo(li);
});
});
});
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Selector</title>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<h2>List of Options</h2>
<ul>
<li><button class="btn">Select</button><span>One</span></li>
<li><button class="btn">Select</button><span>Two</span></li>
<li><button class="btn">Select</button><span>Three</span></li>
<li><button class="btn">Select</button><span>Four</span></li>
</ul>
<h2>List of Selections</h2>
<ul class="mylist">
</ul>
<h2>Array</h2>
<div>Array of Selected Items to Send to DB:</div>
<span ID="s"></span>
</body>
</html>
[출처] https://stackoverflow.com/questions/41775694/add-item-from-array-to-list-when-button-is-clicked-in-jquery
본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
댓글 0
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 수 |
|---|---|---|---|---|
| 9 |
테이블 태그 셀 합치기 그리고, 테이블 안 테이블
| 졸리운_곰 | 2019.03.23 | 566 |
| 8 | 테이블 컬럼 너비 고정 : Fixed Table Cell Width | 졸리운_곰 | 2019.03.21 | 767 |
| 7 | 포지션 css - position (static, relative, absolute, fixed 차이) | 졸리운_곰 | 2019.03.16 | 496 |
| 6 |
DIV 태그를 이용하여 레이아웃 만들기
| 졸리운_곰 | 2019.02.06 | 806 |
| 5 | HTML DIV tag: 세 개의 DIV영역을 가로로 나란히 놓는 방법 - 테이블과 비교 | 졸리운_곰 | 2019.02.04 | 588 |
| 4 |
카드형 디자인/갤러리/리스트 코딩하기.
| 졸리운_곰 | 2018.09.06 | 481 |
| 3 |
[HTML5] span과 div의 차이
| 졸리운_곰 | 2018.06.04 | 800 |
| 2 | HTML div 왼쪽, 오른쪽 배치 | 가을의곰 | 2017.06.10 | 493 |
| 1 |
XPath 이야기
| 졸리운_곰 | 2017.03.20 | 591 |

