3

When I give value as a parameter to function,

such like this,

<html>
  <head>
    <style rel="stylesheet">
      .A { border: 1px solid red; background-color: white }
    </style>
    <script>
      function clicked(v) {
        console.log(v);
      }
    </script>
  </head>
  <body>
    <input type="button" class="A" value="1" onclick="clicked(value)">
  </body>
</html>

It works well.

It shows me v's value.

However, when I give class as a parameter,

such like this,

<html>
  <head>
    <style rel="stylesheet">
      .A { border: 1px solid red; background-color: white }
    </style>
    <script>
      function clicked(c) {
        console.log(c);
      }
    </script>
  </head>
  <body>
    <input type="button" class="A" value="1" onclick="clicked(class)">
  </body>
</html>

It gives me an error.

I want to make it show the button's class option, "A".

How can I make it?

Mamun
  • 66,969
  • 9
  • 47
  • 59
Hoseong Jeon
  • 1,240
  • 2
  • 12
  • 20
  • Possible duplicate of [get class name from element with onclick](https://stackoverflow.com/questions/17060971/get-class-name-from-element-with-onclick) – Mohammad Nov 25 '18 at 06:10

3 Answers3

2

Try with Element.getAttribute()

getAttribute() returns the value of a specified attribute on the element. If the given attribute does not exist, the value returned will either be null or "" (the empty string);

onclick="clicked(this.getAttribute('class'))"

<html>
  <head>
    <style rel="stylesheet">
      .A { border: 1px solid red; background-color: white }
    </style>
    <script>
      function clicked(c) {
        console.log(c);
      }
    </script>
  </head>
  <body>
    <input type="button" class="A" value="1" onclick="clicked(this.getAttribute('class'))">
  </body>
</html>

OR: With Element.className

className gets and sets the value of the class attribute of the specified element.

onclick="clicked(this.className)"

<html>
  <head>
    <style rel="stylesheet">
      .A { border: 1px solid red; background-color: white }
    </style>
    <script>
      function clicked(c) {
        console.log(c);
      }
    </script>
  </head>
  <body>
    <input type="button" class="A" value="1" onclick="clicked(this.className)">
  </body>
</html>

OR: Even you can pass this object so that you can access all the property inside the function as you need:

onclick="clicked(this)"

<html>
  <head>
    <style rel="stylesheet">
      .A { border: 1px solid red; background-color: white }
    </style>
    <script>
      function clicked(c) {
        console.log(c.className);
      }
    </script>
  </head>
  <body>
    <input type="button" class="A" value="1" onclick="clicked(this)">
  </body>
</html>
Mamun
  • 66,969
  • 9
  • 47
  • 59
1

Here is the solution for you. Use DOM element className.

<html>
<head>
 <style rel="stylesheet">
  .A { border: 1px solid red; background-color: white }
 </style>
 <script>
  function clicked(c) {
   console.log(c);
  }
 </script>
</head>
<body>
 <input type="button" class="A" value="1" onclick="clicked(this.className)">
</body>
</html>
onejeet
  • 1,191
  • 6
  • 14
1

You can pass this to the function, and then the function will have access to all of the input element properties, among which is className, but you can also access any other attributes within, without changing the calling mechanism

<html>
<head>
 <style rel="stylesheet">
  .A { border: 1px solid red; background-color: white }
 </style>
 <script>
  function clicked(v) {
   console.log(v.className,v.value,v.type);
  }
 </script>
</head>
<body>
 <input type="button" class="A" value="1" onclick="clicked(this)">
</body>
</html>
Ahmad
  • 12,336
  • 6
  • 48
  • 88