ajax로 name 값 js로 가져오기

yesrin·2023년 7월 20일

Javascript

목록 보기
1/3

html

<img name="imageUrl" th:src="${item.imageUrl}" alt="메뉴 이미지">
<h3 name="productName" th:text="${item.productName}"></h3>
<p name="description" th:text="${item.description}"></p>
<span name="price" th:text="${item.price}">가격: 10,000원</span>

js

$(".add-to-cart-button").on("click", function () {
        const $menuItem = $(this).closest(".menu-item");
        const productId = $menuItem.find("span[name='productId']").text();
        const productName = $menuItem.find("h3[name='productName']").text();
        const price = $menuItem.find("span[name='price']").text();
}

const $menuItem = $(this).closest(".menu-item");
사용해서 가져올 수 있다.

profile
안녕하세요! 틀린 정보는 댓글 달아 주세요.

1개의 댓글

comment-user-thumbnail
2023년 7월 20일

잘 봤습니다. 좋은 글 감사합니다.

답글 달기