Get name of form element

Javascript

Javascript Problem Overview


i have this simple JS for validating form, can someone tell me how to get name of field (you know, name=""), it should be where NameOfSomefield is now :S I tried with someField.tagName but no luck...

function validateForm(){
    var someField = document.forms["nameofofrm"]["someField"].value;
    if (someField==null || someField=="") {
        alert("You cannot leave blank this field: ".NameOfSomefield);
        return false;
    }
}

Javascript Solutions


Solution 1 - Javascript

var name = element.getAttribute("name");

Solution 2 - Javascript

If you want a jQuery approach, you may use:

let elementName = $('#element_id').attr('name')

You can find more information about jQuery selectors here

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSomeoneSView Question on Stackoverflow
Solution 1 - JavascriptDave RagerView Answer on Stackoverflow
Solution 2 - JavascriptCybeXView Answer on Stackoverflow