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

Popular posts from this blog

android - Why am I getting the message 'Youractivity.java is not an activity subclass or alias' -

python - How do I create a list index that loops through integers in another list -

c# - “System.Security.Cryptography.CryptographicException: Keyset does not exist” when reading private key from remote machine -