0

I don't know if this question has been asked here on SO, i just don't know the right word.

I have this input tag:

 <input type = "text" class="inputbox holdout-7"></input>

How do I get the holdout's value of 7 from the class using javascript?

This is because I wanted to add custom attributes but when the page is rendered, my custom attribute is not displayed. Some advised me to put them in a class instead.

For example:

<input type = "text" class = "inputbox" holdout="7"></input>

when the page is rendered, the holdout is not included, therefore I cannot get the value.

7
  • 2
    It's not a value, holdout-7 is a class just like inputbox Commented Mar 26, 2015 at 2:05
  • What are you actually trying to do? Commented Mar 26, 2015 at 2:05
  • i want to store a value to a custom attribute, like holdout = 7. but the page does not render the holdout attribute that I used. it only renders default attributes like size, class, label etc.. Commented Mar 26, 2015 at 2:07
  • 1
    First, <input> elements do not have closing tags, secondly you should prepend data- to any custom attributes e.g. data-holdout="7" Commented Mar 26, 2015 at 2:11
  • 1
    <input type="text" class="inputbox" data-holdout="7"> Commented Mar 26, 2015 at 2:16

1 Answer 1

2
var inputBox = document.querySelector(".inputbox"),
    classname = inputBox.className,
    regEx = /holdout-(\d+)/,
    holdoutValue = classname.match(regEx)[1];

It will return you 7

To set that as attribute in your input box:

inputBox.setAttribute("data-holdout",holdoutValue);

it's recommended to use data-holdout instead of holdout.

Sign up to request clarification or add additional context in comments.

9 Comments

Actually the holdout value is saved in the database and is configured when retrieving it. It is generated when I pass an XML parameter to my form. Where should i put the data-holdout? inside the class? sorry for noobish question.
You must put it in the element as attribute
Like <input type="text" class="inputbox" data-holdout="7"> This will give you the flexibility to retrieve the value easily either in javascript document.querySelector(".inputbox").getAttribute('data-holdout') or in jquery $(".inputbox").data("holdout")
I will try this first mohamedrias. Can this also be used in <field> tag? because I am passing the field as an XML type before the form is generated in the front end.
DOM apis applies to both xml as well as html. So you can use setAttribute and getAttribute in both. When your generating the form use setAttribute to define the holdout
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.