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 TorontoSoftware DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
APD TorontoSoftware DeveloperAuthor 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
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

APD TorontoSoftware DeveloperAuthor 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 TorontoSoftware DeveloperAuthor Commented:
I am not worry about that, but why isnt my code working?
0
APD TorontoSoftware DeveloperAuthor 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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
HTML

From novice to tech pro — start learning today.