-1

I have variables which:

  • display the result (result), and
  • reference the current node (thisNode).

What do I need to change in my code so that it would display the HTML class?

var thisNode = document.body.firstChild;
var result   = document.getElementById("resultOfButton");

result.InnerHTML = thisNode.; 

/* Here, in JS are there any ways like displaying the class name, 
like nodeClass */

Please give recommendations for my code. There may be some errors. Thank you.

var thisNode = document.body.firstChild;
var result = document.getElementById("resultOfButton");
var block = false;

function buttonDown()
  {
  if(block == true)
    {
    thisNode = thisNode.parentElement.firstChild;
    block    = false;
    }
  thisNode = thisNode.nextSibling;
  result.innerHTML = thisNode.nodeName;
  if(thisNode == thisNode.parentNode.lastChild)
    {
    block = true
    }
  }
function buttonUp()  
  { 
  // not done now...     
  }
function buttonEnter()
  {
  thisNode = thisNode.firstChild;
  result.innerHTML = thisNode.c;
  }
function buttonBack()
  { 
  // not done now...     
  }
Mister Jojo
  • 20,093
  • 6
  • 21
  • 40
  • There is no such thing as a "CSS class". Are you asking what HTML classes apply to an element? What CSS rules apply to an element? What CSS rule-sets apply to an element or something else? – Quentin Jan 09 '20 at 12:10
  • 1
    [Pictures of code](http://idownvotedbecau.se/imageofcode) are not very helpful. Put a [mcve] in the question itself. The editor has buttons and [instructions](https://stackoverflow.com/editing-help) to help you format it so it is readable. Provide a [live demo](https://stackoverflow.blog/2014/09/16/introducing-runnable-javascript-css-and-html-code-snippets/) if you're asking about HTML/JavaScript/CSS. – Quentin Jan 09 '20 at 12:11
  • Possible duplicate: https://stackoverflow.com/questions/1227286/get-class-list-for-element-with-jquery – Quentin Jan 09 '20 at 12:12
  • Re edit: Please read the previous comments. Don't just link to *another* picture. Don't ignore questions in comment asking you to clarify exactly what you want. – Quentin Jan 09 '20 at 14:03
  • please provide real code not picture of your code, use MarkDown syntax – Mister Jojo Jan 09 '20 at 14:34
  • I cut out everything that is not needed. Thank you very much for your changes ...(If something goes wrong I will correct) – RokweMICHAEL Jan 09 '20 at 14:36
  • Anything else ? – RokweMICHAEL Jan 09 '20 at 14:48

1 Answers1

1

I think you're asking for the className attribute. I copied your first sample and added some code so you can run it on this page. You'll get the second emoji replaced by the class name of the inserted element.

var thisNode = document.getElementById("thisNode"); // document.body.firstChild;
var result = document.getElementById("resultOfButton");
result.innerHTML = thisNode.className; /*Here, in JS are there any ways like displaying the class name, like nodeClass*/
<div id="thisNode" class="sample-class"></div>
<div id="resultOfButton"></div>

Quoting MDN: "The className property of the Element interface gets and sets the value of the class attribute of the specified element."

nilsandrey
  • 1,030
  • 11
  • 28