最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How can I get HTML element value by JavaScript? - Stack Overflow

programmeradmin0浏览0评论
<div class="gallery_re_form">
  <form action="/?/forms/comment_submit" method="post" accept-charset="utf-8" name="comment_write" id="comment_write" />
      <div style="display:none">
          <input type="hidden" name="ci_t" value="625752bb366f010c56e506e5d0f93822" />
      </div>
      <input type="hidden" name="ehqo_C" id="ehqo_C" value="spam_key" />
      <input type="hidden" name="spam_key" id="spam_key" value="rhkdrhgkwlak!!" />
      <input type="hidden" name="fb8f95190d2b70480acb309f91653d7e4416d4f99a0b67a0b866f06087fdec4588f68fe210675e74919d2ecc50e08f1fd6f75119" id="fb8f95190d2b70480acb309f91653d7e4416d4f99a0b67a0b866f06087fdec4588f68fe210675e74919d2ecc50e08f1fd6f75119" value="b9d3cc45576e30144d8f299fdc61356552989b3aba08238c811b1d3183a104cc2d1d3984af72c4f2eaa2738ca41819d05533731a" />
      <input type="hidden" name="service_code" value="21ac6e96ad152e8f15a05b7350a24759b5606fa191c17e042e4d0175735f4c61d63153c3ce7b4eb89b565a7f6a04ad0667df75f39625ab6fe0816d23f4bed5387546b52d6874b5c201e54df7b5db9187219b048cffc9ef8a106febabfba125eff122df732d9cc52fcaae8b11c42ff5f6fd5ef81901df4103827e7233615992141c180be76852634d53b60c6f911ccdfc762b3db554d8ee36528547bceede30697120c5291acb255a" />
      <br />
      <div class="re_input">
          <div>
              <ul>
                  <li>
                      <input type="text" name="name" id="name" class="re_name re_in" onfocus="this.style.background='#FFFFFF'" title="nickname" />
                  </li>
                  <li>
                      <input type="password" name="password" id="password" class="re_pw re_in" onfocus="this.style.background='#FFFFFF'" title="password" />
                  </li>
              </ul>
          </div>
      </div>
  </form>

I'm trying to rewrite google chrome extensions. I want to get #spam_key s #service_codes value in this HTML response.

I have not used JavaScript.

How can I get HTML element id (spam_key, service_code) and value by JavaScript?

<div class="gallery_re_form">
  <form action="http://gall.dcinside.com/?/forms/comment_submit" method="post" accept-charset="utf-8" name="comment_write" id="comment_write" />
      <div style="display:none">
          <input type="hidden" name="ci_t" value="625752bb366f010c56e506e5d0f93822" />
      </div>
      <input type="hidden" name="ehqo_C" id="ehqo_C" value="spam_key" />
      <input type="hidden" name="spam_key" id="spam_key" value="rhkdrhgkwlak!!" />
      <input type="hidden" name="fb8f95190d2b70480acb309f91653d7e4416d4f99a0b67a0b866f06087fdec4588f68fe210675e74919d2ecc50e08f1fd6f75119" id="fb8f95190d2b70480acb309f91653d7e4416d4f99a0b67a0b866f06087fdec4588f68fe210675e74919d2ecc50e08f1fd6f75119" value="b9d3cc45576e30144d8f299fdc61356552989b3aba08238c811b1d3183a104cc2d1d3984af72c4f2eaa2738ca41819d05533731a" />
      <input type="hidden" name="service_code" value="21ac6e96ad152e8f15a05b7350a24759b5606fa191c17e042e4d0175735f4c61d63153c3ce7b4eb89b565a7f6a04ad0667df75f39625ab6fe0816d23f4bed5387546b52d6874b5c201e54df7b5db9187219b048cffc9ef8a106febabfba125eff122df732d9cc52fcaae8b11c42ff5f6fd5ef81901df4103827e7233615992141c180be76852634d53b60c6f911ccdfc762b3db554d8ee36528547bceede30697120c5291acb255a" />
      <br />
      <div class="re_input">
          <div>
              <ul>
                  <li>
                      <input type="text" name="name" id="name" class="re_name re_in" onfocus="this.style.background='#FFFFFF'" title="nickname" />
                  </li>
                  <li>
                      <input type="password" name="password" id="password" class="re_pw re_in" onfocus="this.style.background='#FFFFFF'" title="password" />
                  </li>
              </ul>
          </div>
      </div>
  </form>

I'm trying to rewrite google chrome extensions. I want to get #spam_key s #service_codes value in this HTML response.

I have not used JavaScript.

How can I get HTML element id (spam_key, service_code) and value by JavaScript?

Share Improve this question edited Mar 3, 2017 at 8:49 Prakhar Trivedi 8,5263 gold badges29 silver badges36 bronze badges asked Mar 3, 2017 at 8:05 the pheonixthe pheonix 731 gold badge1 silver badge6 bronze badges 3
  • Where are id = spam_key value and id = service_code ? – Mihai Alexandru-Ionut Commented Mar 3, 2017 at 8:06
  • @Alexandru-IonutMihai Mistakes in formatting were hiding them from view. Should be fixed now. – gyre Commented Mar 3, 2017 at 8:10
  • 2 Possible duplicate of How to get an input text value in JavaScript – Rajesh Commented Mar 3, 2017 at 8:14
Add a comment  | 

5 Answers 5

Reset to default 5

DOM has a document object , you can use it like this to get the HTML element what you want

document.getElementById("id")

Now to get value, just use value property of this element.

document.getElementById("id").value

You want to retrieve the spam_key element and service_code element's values?

var spam_key = document.querySelector('#spam_key');
var service_code = document.querySelector('input[name=service_code]');

console.log(spam_key.value);
console.log(service_code.value);

You can learn more about selection element from javascript at here

You can get value of an HTML input element using:

var spam_key = document.getElementById('spam_key').value;
var service_code = document.getElementById('service_code').value;

Try this, It should give the value of spam_key

document.getElementById('spam_key').value;

If you have jQuery included you can get it by following: $("#spam_key").val()

发布评论

评论列表(0)

  1. 暂无评论