How to get the html attribute value?

javascript get attribute value
javascript get attribute data
html data attribute
get attribute value from innerhtml javascript
javascript get element by attribute value
html custom attributes
get element by value
jquery get attribute value

I would like to get the value of the "value" attribute on an HTML code wherein class = validated and save it to variable "variationID". How can i code that using java?

Here is the part of HTML code:

<input id="942981037_3-4" type="text" size="35" value="533" data-valid="-0123456789" class="validated"/>

File attached here:

Here is the initial code I created:

String html = prev.getResponseDataAsString(); // get response from your sampler
String variationID;
Integer b;
for (int i = -1; (i = html.indexOf("class=\"validated\"", i + 1)) != -1; i++) {
    b = i;
variationID = html.substring(b,b+3);
int res = Integer.parseInt(variationID) + 1;
variationID = res.toString();;

There are several HTML parser libraries (jsoup for example) but if youo need to use only the standard Java you can use a DocumentBuilder to parse your page

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;    
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();

    StringBuilder xmlStringBuilder = new StringBuilder();
    xmlStringBuilder.append("<html> <body><input id=\"942981037_3-4\" type=\"text\" size=\"35\" value=\"533\" data-valid=\"-0123456789\" class=\"validated\"/></body></html>");
    ByteArrayInputStream input = new ByteArrayInputStream(
    Document doc = builder.parse(input);

The example exactly fit your line but with an helper method you can extract any other element.

Element.getAttribute(), To get the values from non-standard attributes we can use getAttribute() method. Syntax: It returns the string containing the attribute's value. Object.getAttribute(  For <button>, <input> and <option> elements, the value attribute specifies the initial value of the element. For <li> elements, the value attribute sets the value of the list item (for ordered lists). The next list items will increment from that value. For <meter> elements, the value attribute specifies the current value of the gauge.

Why don't you use Regular Expressions instead. You can simply write a regular expression to extract the attribute from the input tag. Refer the code below to extract the value of the attribute.

    String html = prev.getResponseDataAsString(); // get response from your sampler
    String variationID;

    // Pattern for finding input tag with class validated
    String input = "<input id=\"942981037_3-4\" type=\"text\" size=\"35\" value=\"533\" data-valid=\"-0123456789\" class=\"validated\"/>";
    // Compiling the pattern
    Pattern pattern = Pattern.compile("\\<input.*(class=\".*validated.*\")?.*value=\"([\\w\\d]+)?\".*(class=\".*validated.*\")?.*/\\>");
    // Passing the input string to the pattern to start matching
    Matcher matcher = pattern.matcher(input);

    // Iterate through all the matches in given string
    while(matcher.find()) {
        // If a match is found value will be found in group 2
        variationID =;

Element.getAttribute( ): return the string value of a named attribute , Node values and Element Attributes are different parts of an html tag. So, you have to use element.value instead. This is a an example, to show  The value attribute specifies the value of an <input> element. The value attribute is used differently for different input types: For "button", "reset", and "submit" - it defines the text on the button

If You don't want to use RegEx, You can use more simple Java with String split() method.

        String one = "<input id=\"942981037_3-4\" type=\"text\" size=\"35\" value=\"533\" data-valid=\"-0123456789\" class=\"validated\"/>";
        int variationID = -1;
        if (one.contains("value=\"")) {
            String part = one.split("value=\"")[1]; //Take everything from value=" forward
            String val = part.split("\"")[0]; //take stuff until first "
            try {
                variationID = Integer.parseInt(val);
            } catch (Exception e) {
                System.out.println("Nope. I guess, the value is wrong");

Here You just split the string from value=" forward. And when you get that, you can split the remaining part by the " and you will be left with what's in the middle.

HTML, To get the value of an attribute on a specified element, you call the getAttribute() method of DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>JS  We Suggest: Always Use Lowercase Attributes. The HTML standard does not require lowercase attribute names. The title attribute (and all other attributes) can be written with uppercase or lowercase like title or TITLE. However, W3C recommends lowercase attributes in HTML, and demands lowercase attributes for stricter document types like XHTML.

How to get the value of an attribute in Javascript, Using jQuery's .attr() method to get the value of an element's attribute has two main boolean attributes, consider a DOM element defined by the HTML markup  When a single parameter is passed to the.attr () function it returns the value of passed attribute on the selected element.

JavaScript getAttribute(): Get the Value of an Attribute on an Element, They can have any value. They are case-sensitive (write elem.nodeType , not elem.NoDeTyPe ). HTML attributes. In HTML  HTML Reference HTML by Alphabet HTML by Category HTML Browser Support HTML Attributes HTML Global Attributes HTML Events HTML Colors HTML Canvas HTML Audio/Video HTML Character Sets HTML Doctypes HTML URL Encode HTML Language Codes HTML Country Codes HTTP Messages HTTP Methods PX to EM Converter Keyboard Shortcuts

.attr(), In the above example, we are getting the data-foo attribute value and appending in that front of paragraph p tag of HTML. As you saw we used  element.getAttribute ("value") returns value which was set in the markup, which is not necessarily same as element.value. Also, value attribute of an element is only synchronized one way - from markup to the object and vice versa doesn't happen. So, if you want to get the value that is set programmatically, you need to write