javascript - Fetch "href" Information On Clicking a Button -
i have extract "href" information of button if being clicked. html tag corresponding button is:
<a class="donate" target="_self" rel="" href="/donate/donate-monthly">
how can fetch same using javascript?
first should select element var el = document.getelementbyid("yourid")
if have id
attribute attached or var els = document.getelementsbyclassname("yourclass")
.
note getelementbyid
return single element if found getelementsbyclassname
return of elements class if have more 1 array.
after have selected element lets var el = document.getelementbyid("yourid")
can extract href getattribute()
method. code if select id like
var el = document.getelementbyid("someid"); var href = el.getattribute("href"); console.log(href); // or whatever want
or if select class , sure have 1 element class can do
var els = document.getelementsbyclassname("donate"); var el = els[0]; // first element class var href = el.getattribute("href"); console.log(href); // or whatever want
note: if there more elements should find correct index of element need.
about click part should attach eventlistener can check when extract href
this:
el.addeventlistener("click", function(){ // el selected in examples above // extract href });
Comments
Post a Comment