简体   繁体   中英

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

<input id="email" name="email" type="text" value="ooooo@gmail.com">

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>

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

尝试

console.log($('#email').val());

Try an alert...

http://jsfiddle.net/nemb666L/

var email = $("#email").val();
alert(email);

这会将整个对象“打印”到控制台

console.log("input[id='email'] - %o", document.getElementById('email')); 

没有 jQuery:

console.log(document.getElementById('email').value);

有用

console.log(email.value)

在 Chrome 开发工具控制台中删除 .value:

document.getElementById('#id')

If email (your id) is not defined, just:

email.innerHTML

在此处输入图片说明

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM