-3

I found this html code on a website which is a click button and I want to learn how to click on this button using Javascript. Please someone help me to understand how to click such buttons. I'm trying this from past one hour.

This is the button which I want to click:

 <div class="Button">
<a href="javascript:void(0)" class="button yellow-btn addToCart trackEvent" rel="Thebutton" data-action="Tap">Tap this button</a>


<div id="ButtonTwo" class="ButtonTest2">
<a href="javascript:void(0)"><p class="MeButton" id="ButtonID">Final button test</p></a></div>

this is not my code. Its from a website.

4
  • It's not a button, it's a hyperlink, that refers to nothing. You can select the <a> tag with a normal querySelector and then get the href property from it, but javascript:void(0) doesn't do anything, so following that hyperlink doesn't do anything. Commented Sep 21, 2017 at 13:24
  • Your HTML is not valid and you haven't show what you tried regarding JavaScript. What are you trying to do with that code ? It feels like you are going on a way far longer and far more complicated than it needs to be ... Commented Sep 21, 2017 at 13:31
  • @Shilly then how do i click on that button? I'm literally confused. :( Commented Sep 21, 2017 at 14:00
  • @Wndrr i copied this code from a website where I was trying to click on buttons using Javascript, but I couldn't click on this button with: $("# button").trigger("click") Commented Sep 21, 2017 at 14:01

2 Answers 2

1

You need to close the first div with a closing tag. Below is the updated code with the closing tag.

<div class="Button">
    <a href="javascript:void(0)" class="button yellow-btn addToCart trackEvent" rel="Thebutton" data-action="Tap">Tap this button</a>
</div>

<div id="ButtonTwo" class="ButtonTest2">
    <a href="javascript:void(0)"><p class="MeButton" id="ButtonID">Final button test</p></a>
</div>

What is the purpose of enclosing your a tag in a div ? Can't you use only the a ?

You do not have any button in the HTML snipper shown, only div, a, and p

A button would be a <button>.

to click them using your current code you can use

<script>
    document.getElementsByClassName("button")[0].click();
    document.getElementsById("ButtonID").click();
</script>

A correct, valid and clear code would be

<!-- What is this button meant to do ? Can you use an actual button or do you HAVE to use a <a> ?
<button id="clickUsingJs" class="button yellow-btn addToCart trackEvent" rel="Thebutton" data-action="Tap">
    Tap this button
</button>
<script>
    document.getElementById("clickUsingJs").click();
</script>
Sign up to request clarification or add additional context in comments.

Comments

-1
<div class="Button" id="button1" onclick ="call some javascript method">
<a href="javascript:void(0)" class="button yellow-btn addToCart trackEvent" rel="Thebutton" data-action="Tap">Tap this button</a>


<div id="ButtonTwo" class="ButtonTest2" onClick="call some javascript method">
<a href="javascript:void(0)"><p class="MeButton" id="ButtonID">Final button test</p></a></div>

try this

2 Comments

div id="ButtonTwo" class="ButtonTest2" onclick="call some javascript function"

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.