Reputation: 447
I am using an external car dealership calculator script, and in order to fit it to my website I need to change the value of objectPrice, based on my page content.
How can I with jQuery change the value of objectPrice?
I have tried following code, but that did not work:
jQuery(document).ready(function() {
jQuery("#objectPrice").attr("objectPrice", "500");
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div
id="externalcalculator"
partnerExternalDealerId="1234"
objectType="1"
make="Audi"
model="A6"
variant="3,0 TDI"
mileage="600"
firstregistrationdate="2014-11-25"
objectPrice="225000"
showaspricelabel="false">...</div>
Upvotes: 0
Views: 161
Reputation: 12864
Your id is not correct, you can use jQuery("#externalcalculator")
.
But there is another issue, you are using camelCase notation for your attribute. jQuery .attr()
method transform this attribute into lowercase.
You can use native javascript with .setAttribute()
to change attribute with camelCase.
Like :
jQuery("#externalcalculator").get(0).setAttribute("objectPrice", "500");
Note: Per http://www.w3.org/ “XHTML documents must use lower case for all HTML element and attribute names.”
Upvotes: 2
Reputation: 44
Mads Hjorth, based on you snippet above, your're very close to get where you want. You are selecting the wrong div id, you want to change to
jQuery(document).ready(function(){ jQuery("#externalcalculator").attr("objectPrice", "500");});
This should make the magic.
Upvotes: -1