0

i need to send the input fields "data-validation" to javascript function.

form is

 <input type="text" name="fname" id="fname"  data-validation="length" onchange="validate(this);" required />

Javascript is

 function validate(value)
    {
        var value=value.value;
        var validation=value.data-validation;
        alert(value +"  "+ validation);
        return false;
    }

i can't get the value of data-validation in javascript.

1
  • avoid using same names for the parameters and variables var value1 =value.fname Commented Mar 14, 2014 at 7:02

3 Answers 3

2

please review this code getAttribute() is used to get attribute of element

function validate(ele)
        {
            var value=ele.value;
            var validation=ele.getAttribute("data-validation")
            alert(value +"  "+ validation);
            return false;
        }

Demo here http://jsfiddle.net/GgfM3/

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

1 Comment

Please explain your answer, don't just post code.
1

Attribute of an element cannot be accessed just through the dot. You should use "getAttribute("myAttribute")" in javascript and "attr("myAttribute")" with jQuery.

How about using jQuery? The post here suggests that "onchange is only triggered when the control is blurred". The jQuery .on can monitor value changes in the input.

$("#fname").on("input", function(){
    var value=$(this).val();
    var validation=$(this).attr("data-validation");
    alert(value +"  "+ validation);
}

Check example here.

Comments

0

You can retrive the Attribute values using the getAttribute('value') method.

In you example you can get the value using

function validate(data)
    {           
       var dataFromTextbox = data.getAttribute("data-validation")
       alert(dataFromTextbox);            
    }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.