0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

JavaScriptメソッドで出来ること

Last updated at Posted at 2021-04-18

#はじめに
初学者の私が、アクティブラーニング兼備忘録の為にJavaScriptのメソッドをまとめる記事です。
少しずつ追記していきます。

#目次

  • getElementById
    • 基本構文
    • innerHTMLで活用
    • onClickで活用

#getElementById
getElementByIdメソッド
HTMLタグで指定した任意のIDにマッチするドキュメント要素を取得する。
これが一番よく使う気がします。

##基本構文

<html>
  <body>
    <p id='hey'>Hello world</p>
    <script>
      console.log(document.getElementById('hey'));
    </script>
  </body>
</html>
<p id='hey'>Hello world</p>

##innerHTMLで活用

<html>
  <body>
    <p id='hey'>こんにちは</p>
    <input type='button' value='Click' onclick='myfunc()'>

    <script>
      let myfunc = () => {
        const myp = document.getElementById('hey');
        myp.innerHTML = 'こんばんは';
      }
    </script>
  </body>
</html>

ボタンを押すと「こんにちは」から「こんばんは」になる

##onClickで活用

<html>
  <body>
    <p>push button</p>
    <input type='button' id='myid' value='Click'>

    <script>
      let mybutton = document.getElementById('myid');
      let myfunc = () => {
        mybutton.value = 'Hello world';
      }
      mybutton.onclick = myfunc;
    </script>
  </body>
</html>

ボタンを押すと、ボタンに書いてある「Click」が「Hello world」になる

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?