Saad
Saad

Reputation: 28486

How can I access the canvas element without an id?

Using CasperJs, I'm trying to do some testing on canvas, by grabbing it and using canvas.toDataURL();. However, the canvas does not have an id, the code looks something like this:

<div id= 'derp' ...>
<canvas ...> </canvas>
</div>

Can I still get the canvas using something like

var canvas = document.getElementById(????);

or is there a better way of grabbing the canvas?

Upvotes: 8

Views: 15403

Answers (2)

You can also list all the canvas with this:

document.getElementsByTagName('canvas')

Upvotes: 6

SLaks
SLaks

Reputation: 887195

You can use CSS selectors:

document.querySelector('#derp canvas')

Upvotes: 14

Related Questions