Html Input Attribute for Extra Information

Hi Experts,

Is there an attribute in html input tag where you can store some extra information. In VB I know this attribute is actually called tag

In HTML/JavaScript, this is my goal: I have a lot of input elements, where I need to do some calculations based on user input - a calculator if you will, with rates, quantity, etc. so in my JS I am doing total = rates x quantity, simple.

Where my extra information needs to come in is that for this particular example I will have 18 user input fields, and this extra info would simply be the text box's name in a human description, opposed to txtFamQty, but it will be Family Quantity, which I can use in an alert to inform the user what textbox to fix. Also, not every textbox will have an association label because the textboxes will be in a table, identified by the column and row headings.

Thank you
APD TorontoAsked:
Who is Participating?
 
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
I do this all the time as do javascript libraries like angualr or knockout.  You can add a data- element like data-price to an input or div.  In the sample below, click on the field and you get a message that is in the data-msg attribute and the price from the div that holds the item.  

Where this type of thing is very practical is ecommerce.  For most things, you need to have the price available, but you never want to let the user manipulate the price or final cost.  Of course the down side to that is the page can be scrapped if you have a lot of data.

http://jsbin.com/jofiratihi/1/
<!DOCTYPE html>
<html>
<head>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
  <script>
    $(function(){
      $('input[name="qty"]').on('click',function(){
        var msg=$(this).attr('data-msg');
        var price=$('div#itemname1').attr('data-price');
        alert(msg+' the price is '+price);
      });
    });
  </script>
  <meta charset="utf-8">
  <title>Scott Q_28656011</title>
</head>
<body>
<input name="qty" data-msg="This is qty">
  <div id="itemname1" data-price="12">t-shirt</div>
</body>
</html>

Open in new window

0
 
Dave BaldwinFixer of ProblemsCommented:
Not in standard HTML/JavaScript that I know of.  There are some libraries that can add data attributes to elements.  On one set of pages that I maintain, the validation routine goes thru the elements that are checked and if there is a problem, it puts up an alert with the message and then it puts the 'focus' on that element which takes the user directly to the element that needs attention.
0
 
APD TorontoAuthor Commented:
what do you mean:

Of course the down side to that is the page can be scrapped if you have a lot of data.
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
APD TorontoAuthor Commented:
With your example I came up with the code below, but my end result is Undefined with 12 value. so, my "data" is not being read.

<!DOCTYPE html>
<html>
<head>

  <title>Scott Q_28656011</title>
</head>
<body>
<input name="qty" data="This is qty" onChange="changed(this);">
  
<script type="text/javascript">
    function changed(element){
        var qty = element.value;
        var data = element.data;
        
        alert (data + ' with ' + qty + ' value.');
        
    }
</script>

</body>
</html>

Open in new window

0
 
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
If you have a lot of data that people want, they can write a script to read your rendered html.  This is called scraping.

One way to prevent that is to use ajax and grab only when needed.  If you are not using a big list of data I wouldn't worry about it.
0
 
APD TorontoAuthor Commented:
I am not worry about that, but why isnt my code working?
0
 
APD TorontoAuthor Commented:
Got it!
<!DOCTYPE html>
<html>
<head>

  <title>Scott Q_28656011</title>
</head>
<body>
<input name="qty" data-type="This is qty" onChange="changed(this);">
  
<script type="text/javascript">
    function changed(element){
        var qty = element.value;
        var data = element.dataset.type;
        
        alert (data + ' with ' + qty + ' value.');
        
    }
</script>

</body>
</html>

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.