I don't have jQuery and I want to know how can I do the same thing with vanilla javascript. Any help?
$('meta[name="csrf-token"]').attr('content')
document.querySelector('meta[name="csrf-token"]').getAttribute('content');
That's how you do it.
meta
elements actually have a content
property, so you can simply write:
document.querySelector('meta[name=csrf-token]').content
console.log(
document.querySelector('meta[name=csrf-token]').content
)
<meta name="csrf-token" content="example-content"/>
You can also use getElementsByName
document.getElementsByName('csrf-token')[0].getAttribute('content')
Use querySelector()
method to get the element and use getAttribute()
method to get the attribute value from the element.
document.querySelector('meta[name="csrf-token"]').getAttribute('content')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With