Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why val() is not returning the value of <button value="value">?

I read that val() returns value of input element but here in my code it is not working.
What

<script>
$(document).ready(function(){
    var a = $("button").val();
    alert(a)
});
</script>
<button value="text"/>

Thanks in advance

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Use button element and call .html()

<button>value1</button>

or use input element and call .val()

<input type="button" value="value1"/>

Then access them as

// jquery
<script>
$(document).ready(function(){
    var btn = $("button").html();
    var inp = $("input").va();
    alert('button: '+btn)
    alert('input: '+inp)
});
</script>

//html
<input type="button" value="value1"/>
<button>value1</button>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading