6

ERROR in src/app/dashboard.component.ts(49,15): error TS2339: Property 'style' does not exist on type 'Element'. src/app/dashboard.component.ts(50,16): error TS2339: Property 'textContent' does not exist on type 'EventTarget'.


This works as a javascript function, but the above are the errors I get when I try to use this in typescript. I have tried changing var to let and adding . any other suggestions? Thanks.

dashboard.ts

var w = document.getElementById('wrapper');
var button = document.getElementById('randomize');
var image = w.children; // inner elements, your quotes divs

// a function to hide all divs
var hideDivs = function(divs) {
  for (var div of divs) {
    div.style.display = 'none';
  }
}

hideDivs(image); // hide all initially

// on click
button.addEventListener('click', function(event) {
  var rnd = Math.floor(Math.random() * image.length); // get random index
  hideDivs(image); // hide all quotes
  image[rnd].style.display = 'block'; // show random quote
  event.target.textContent = 'Click one more time!'; // set button text. event.target is the button you've clicked
})

dashboard.html

  <div id="wrapper">
  <div class="image" img scr='../../assets/mood.jpg'></div>
  <div class="image" img scr='../../assets/mood.jpg'></div>
  <div class="image" img scr='../../assets/mood.jpg'></div>
  <div class="image" img scr='../../assets/mood.jpg'></div>
  <div class="image" img scr='../../assets/mood.jpg'></div>
</div>
<button id='randomize'>Next</button>

1 Answer 1

6

The problem is that target is typed as EventTarget, and an element of image which is a HTMLCollection is typed as Element. Both of the properties you want to access are defined on HTMLElement. The simplest thing you can do is to use a type assertion to tell the compiler that in your case both of these are actually HTMLElement

// on click
button.addEventListener('click', function (event) {
    var rnd = Math.floor(Math.random() * image.length); // get random index
    hideDivs(image); // hide all quotes

    (image[rnd] as HTMLElement).style.display = 'block'; // show random quote
    (event.target as HTMLElement).textContent = 'Click one more time!'; // set button tqext. event.target is the button you've clicked
})

Yo might also want to type hideDivs correctly as well :

// a function to hide all divs
const hideDivs = function (divs : HTMLCollection) {
    for (var div of divs) {
        (div as HTMLElement).style.display = 'none';
    }
}
Sign up to request clarification or add additional context in comments.

7 Comments

Update hideDivs() function as well. ;)
@Durga true, that did not cause an error (since divs is implicitly typed as any) . Updated the answer :)
Thank you for your help. That compiles successfully but now all divs appear and the error within the console.log is "ERROR TypeError: Cannot read property 'children' of null"
@KatieKennedy strange... the onlu place you use children is w.children, i did not touch that .. where does w come from ? are you sure it is not undefined ?
w is used to get the ID of the wrapper enclosing the images. var w = document.getElementById('wrapper');
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.