Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print a value in console.log base on ID of an element?

<input id="email" name="email" type="text" value="[email protected]">

How do I get value of id=email and print it out to my console or anywhere for testing purposes ?

I tried, but no luck :(

script type="text/javascript">
    document.getElementById('email').innerText;
</script>
like image 762
iori Avatar asked Dec 20 '22 09:12

iori


2 Answers

There are two types of elements

  1. Block - these can be accessed with - .innerHTML or .html() in jquery like div's span's etc
  2. Non Block Elements - these can be accessed by .value or .val() in jquery, like input types etc

all you need o do is

console.log($('#email').val())  //jquery

or 

console.log(document.getElementById('email').value); // javascript
like image 107
Ekansh Rastogi Avatar answered Feb 24 '23 05:02

Ekansh Rastogi


try

console.log($('#email').val());
like image 42
Yosoyke Avatar answered Feb 24 '23 03:02

Yosoyke