0

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.

Mark Schultheiss
  • 32,614
  • 12
  • 69
  • 100
marko
  • 3
  • 2
  • You can't change the source code once it's been parsed. "This is working for me" -- no, it's not; you have a typo in your code. – Heretic Monkey Dec 28 '22 at 17:31
  • Note: I created a snippet of your code BUT your HTML as presented is invalid (as in your JavaScript/jQuery code) SO I voted to close because of these typo's – Mark Schultheiss Dec 28 '22 at 17:51
  • Does this answer your question? [Set the value of an input field](https://stackoverflow.com/questions/7609130/set-the-value-of-an-input-field) – Heretic Monkey Dec 28 '22 at 17:56

2 Answers2

0

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"?

  • Yes, but not change in source. Just visual in input. – marko Dec 28 '22 at 17:40
  • Yes, butt no change in source, when I go to inspect. I need in inspect too. Thanks. – marko Dec 28 '22 at 17:41
  • @marko what exactly do you mean as source? Do you mean data-value of an input? – Matej Pakán Dec 28 '22 at 17:46
  • When I click change working, when I click the changes work, but I want them to change automatically in Inspect. Click on inupt value right click and go to Inspect. I hope you understand – marko Dec 28 '22 at 17:49
  • @marko When you click Inspect, in the right pane, select Properties. If you look at the `value` property, you'll find it has changed. Thus, "Inspect" has changed, as desired. – Heretic Monkey Dec 28 '22 at 17:51
-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" />
GMKHussain
  • 3,342
  • 1
  • 21
  • 19