-1

I have one onclick as below

<div class="tablinks active" onclick="openmarket(event,this)" data-uid="1">

openmarket function like below

function openmarket(evt, obj) {
    var i, tabcontent2, tablinks2;
    tabcontent2 = document.getElementsByClassName("tabcontent2");
    for (i = 0; i < tabcontent2.length; i++) {
        tabcontent2[i].style.display = "none";
    }
    tablinks2 = document.getElementsByClassName("tablinks2");
    for (i = 0; i < tablinks2.length; i++) {
        tablinks2[i].className = tablinks2[i].className.replace(" active", "");
    }
    document.getElementById(obj.getAttribute('data-uid')).style.display = "block";
    evt.currentTarget.className += " active";
}

Here in this function I have passed 2 parameters,
1) event
2) object

now I try to click this div by JS like below.

$("div [data-uid='1']").click();

but it won't work because I guess parameter evt is not valid or mismatch from what it should be.

Can anyone help me ?

Thank you.

Edit I'm getting following error.

Cannot read property 'className' of undefined

and error line is evt.currentTarget.className += " active";

11
  • may I know the reason for voting close ? Commented Jun 19, 2017 at 8:20
  • why don't you add a callback to the click and pass event and this to the openmarket function Commented Jun 19, 2017 at 8:22
  • @SabirAmeen, can you please explain how ? Commented Jun 19, 2017 at 8:23
  • 1
    try to use evt.target.className. Commented Jun 19, 2017 at 8:32
  • 1
    This is expected behaviour as your not actually clicking the div but instead using jQuery click function to simulate the click. Both are different. This might help you stackoverflow.com/questions/11127908/… and yes evt.target.className will work in both cases Commented Jun 19, 2017 at 8:32

2 Answers 2

-1

Your selector query is wrong

change $("div [data-uid='1']").click();

to $("div[data-uid='1']").click();

this will return a jqueryclick click event not a mouse click event

Sign up to request clarification or add additional context in comments.

Comments

-1

You can use simple javascript :

It works well. I fthere is only one div, use and id instead of a class.

<!DOCTYPE html>

<html>

<head>
  <meta charset="UTF-8">
  <title>Insert title here</title>
</head>
<!--I wish to append it here-->
<body>

    <div  class="tablinks active uid1" onclick="openmarket(event,this)" 
   data-uid="1" style="height:30px;width:50px;background-color:red;">
    </body>
</html>

<script type="text/javascript">
function openmarket(evt, obj) {
    var i, tabcontent2, tablinks2;
    tabcontent2 = document.getElementsByClassName("tabcontent2");
    for (i = 0; i < tabcontent2.length; i++) {
        tabcontent2[i].style.display = "none";
    }
    tablinks2 = document.getElementsByClassName("tablinks2");
    for (i = 0; i < tablinks2.length; i++) {
        tablinks2[i].className = tablinks2[i].className.replace(" active", "");
    }
    document.getElementById(obj.getAttribute('data-uid')).style.display = "block";
    evt.currentTarget.className += " active";
}
var list=document.getElementsByClassName("uid1");
for(var i=0;i<list.length;i++){
    list[i].click();
}
</script>

Comments

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.