-3

I am trying to access the below tag class="topbar"

<div id="swagger-ui">
<section data-reactroot="" class="swagger-ui swagger-container">
<div class="topbar">
<div class="wrapper">
<div class="topbar-wrapper">

I have tried this:

   var x=document.getElementById("swagger-ui");
   var y=x.getElementByClassName("swagger-ui swagger-container");
   var z=y.getElementByClassName("topbar");

Also,

How can I set value in js? I have to set the value for input type text

<div class="wrapper"><label>Value:</label><section class=""><input type="text"></section></div>
  • 2
    And what happened when you tried that? – Sergio Tulentsev Jul 18 '20 at 12:08
  • 1
    There's no `.getElementByClassName()` method. It's `.getElementsByClassName()` (plural). – Andreas Jul 18 '20 at 12:09
  • [What do querySelectorAll and getElementsBy* methods return?](https://stackoverflow.com/questions/10693845/what-do-queryselectorall-and-getelementsby-methods-return) – Andreas Jul 18 '20 at 12:11
  • 3
    Your biggest takeaway here should be that it's time to familiarize yourself with your browser's debugging tools (press F12 in Chrome). The error message on the development console therein would have shown you the problem. – David Jul 18 '20 at 12:17

3 Answers3

1

You can do something like the following:

var topbar = document.getElementsByClassName('topbar');

or

var topbar = document.querySelector('.topbar');
Omid Nikrah
  • 2,444
  • 3
  • 15
  • 30
0

You're code is almost perfect, but you need to spell getElementByClassName correctly, and add an indexer for getElementsByClassName. Try adding [0] after each getElementsByClassName function. Like this: getElementsByClassName[0]

horrible
  • 117
  • 1
  • 8
0

try to use querySelector

var x=document.querySelector("#swagger-ui");
var c=document.querySelector(".swagger-ui");
var y=document.querySelector(".swagger-container");
Hassan Al Najjar
  • 324
  • 1
  • 4
  • 13