skip to Main Content

I need show my newprice="" in input value, and in source.

$(".uzmi").on("click", function(e) {
  $("inid").val($(this).attr('newprice'));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div data-value="VALUE 1" class="uzmi product-variation-colors__item helmet-variations" id=" " codeproduct=" " newprice="6043" variation_id="1407" change_img(0) "> Div 1</div>
<div data-value="VALUE 1 " class="uzmi product-variation-colors__item helmet-variations " id=" " codeproduct=" " newprice="2323 " variation_id="1407 " change_img(0)"> Div 2</div>
<div data-value="VALUE 1" class="uzmi product-variation-colors__item helmet-variations" id=" " codeproduct=" " newprice="9999" variation_id="1407" change_img(0) "> Div 3</div>
<input type=" " id="inid " name="variation_id " class="variation_id " value="6041 " />

This working for me, but I want my value to change in the source as well, not only visually.

2

Answers


  1. You need to add "#" within you click function like this, because you are selecting by id:

    $(".uzmi").on("click", function(e) {
           $("#inid").val($(this).attr('newprice'));
     });
    

    And to the second part of your question, what do you mean by "source"?

    Login or Signup to reply.
  2. inid.setAttribute("value", price) added this line to update DOM too

    Solution with Vanilla JavaScript

    const inid = document.getElementById('inid')
    
    const getPrice =(e)=> {
      let price = e.getAttribute('newprice')
      e.setAttribute('source', 'clicked')
      
      inid.value=price;
      inid.setAttribute("value", price)
    }
    [source="clicked"] { color: green }
    <p>Click on DIV</p>
    
    <div onclick="getPrice(this)" data-value="VALUE 1" class="uzmi product-variation-colors__item helmet-variations" id=" " codeproduct=" " newprice="6043" variation_id="1407" change_img(0)"> Div 1</div>
    
    <div onclick="getPrice(this)" data-value="VALUE 1" class="uzmi product-variation-colors__item helmet-variations" id=" " codeproduct=" " newprice="2323" variation_id="1407" change_img(0)"> Div 2</div>
    
    
    <div onclick="getPrice(this)" data-value="VALUE 1" class="uzmi product-variation-colors__item helmet-variations" id=" " codeproduct=" " newprice="9999" variation_id="1407" change_img(0)"> Div 3</div>
    
    
    <input type="" id="inid" name="variation_id" class="variation_id" value="6041" />
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search