I quite often do this:
<input type="text" id="whatever" quantity="2" price="15" />
which causes VS2010 to issue the above warning -- I can live with the warnings because the inherent value in being able to hang data on form fields (which allows me later to pick them up for use in code e.g. $('#whatever').attr('price')
, or simply document.getElementById('whatever').price
) makes the nuisance warnings worth bearing. However, I was wondering what the canonical approach to this problem is...
anyone?
TIA - ekkis
I quite often do this:
<input type="text" id="whatever" quantity="2" price="15" />
which causes VS2010 to issue the above warning -- I can live with the warnings because the inherent value in being able to hang data on form fields (which allows me later to pick them up for use in code e.g. $('#whatever').attr('price')
, or simply document.getElementById('whatever').price
) makes the nuisance warnings worth bearing. However, I was wondering what the canonical approach to this problem is...
anyone?
TIA - ekkis
Share Improve this question asked Sep 21, 2011 at 20:20 ekkisekkis 10.3k13 gold badges61 silver badges113 bronze badges2 Answers
Reset to default 6You should replace quantity="2" price="15"
with data-quantity="2" data-price="15"
-- which has the added bonus of making the values accessible through jQuery's .data()
method:
alert( $('#whatever').data('price') ); // displays 15
http://blog.jquery./2011/05/03/jquery-16-released/
If you're using jQuery, you can store those variables in the object itself with .data()
:
$('#whatever').data('quantity', 2);
But I suggest splitting up your <input />
into multiple <input />
s if you absolutely need to keep the information in the HTML file:
<input type="hidden" id="whatever-quantity" value="2" />
<input type="hidden" id="whatever-price" value="15" />
But for plete HTML5 awesomeness, just prepend data-
to your attributes:
<input type="text" id="whatever" data-quantity="2" data-price="15" />