There is a form with 4 input fields. When any one of the input fields is kept blank and the submit button is clicked, an error popup is displayed for the respective field mentioned in the error text. Whenever any field is NOT kept blank and the submit button is clicked, an attribute 'value' is added to the field tag.
I want to count the number of fields that are kept blank after clicking on the submit button.
My code:
public int getcountBlankFields()
{ int count=0;
if(txt_Field1.getAttribute("value")==null)
{ count++; }
if(txt_Field2.getAttribute("value")==null)
{ count++; }
return count;
}
count does not increase as the condition is not getting verified. Can someone give me Java code to find if a particular attribute exists in an element?
Convert your above code to Javascript. Below code will give the glance of how it should be.
And yes you are mixing two languages. Java is not same as Javascript.
Key differences between Java and JavaScript:
function myFunction() { var count = 0; var x = document.getElementById("input1").hasAttribute("value"); document.getElementById("demo").innerHTML = x; if (x) { count++; } alert(count); }
<input type="text" id="input1" value="sample" /> <button id="myBtn" onclick="myFunction()">Try it</button> <p id="demo"></p>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.