0

I am using following segment of code to add onclick to anchor tag but apparently it is not working.

var address_edit = $('.edit-user a').attr('href'));
$('.edit-user a').prop('onclick', 'Popup("address_edit","Edit","900","500");')

What I want:

This is my code on inspect element:

<div class="edit-user">
    <a href="example.com/nokidding">No kidding</a>
</div>

This is what I need it to be:

<div class="edit-user">
    <a onclick='Popup("address_edit","Edit","900","500");' href="example.com/nokidding">No kidding</a>
</div>
5
  • When there is " href " in anchor it ll be redirected to another page. So its of no use writing " onclick ". Commented Jul 22, 2016 at 9:24
  • Just remove the href attribute in your anchor tag. Commented Jul 22, 2016 at 9:33
  • I kept href as fallback Commented Jul 22, 2016 at 9:36
  • You need show popup or to redirect ?? You cannot do the both in this page. Commented Jul 22, 2016 at 9:41
  • Your question clearly says that you wanted to add an onclick attribute to <a> using javascript. But the answers are not related to it. Can someone help me understand this please ? Commented Jul 22, 2016 at 9:52

6 Answers 6

1

Add click event like this :

$('.edit-user a').click(function(e) {
    e.preventDefault();
    Popup("address_edit","Edit","900","500");

    // I don't know if you need to redirect after popup open
    window.location.href = "example.com/nokidding";
})
Sign up to request clarification or add additional context in comments.

Comments

0

It is not prop, it is attr, because onclick is a attribute, not a property. But I would add a click event listener with jQuery to the element and execute your action there in a callback:

$('.edit-user a').click(function(e) {
    // this stops the 'href' of the anchor from being executed
    // if you want the user to follow the 'href' after click, remove the line
    e.preventDefault();

    Popup("address_edit", "Edit", "900", "500");
});

1 Comment

thanks, it works, i didnt knew I had to dynamically bind this, very helpful community
0

It's attr not prop. You can handle it like this:

$('.edit-user a').on('click', function() {
    Popup("address_edit", "Edit", "900", "500");
});

Comments

0

You need to use dynamic biding for this because you are creating element dynamically. Use below

$(document).on('click', '.edit-user a', function() {
Popup("address_edit", "Edit", "900", "500");
});

Comments

0

Please confirm my demo :

$('.edit-user a').click(function(e) {
    e.preventDefault();
    var href = $(this).attr('href');
    console.log(href);
    //Popup(href, "Edit", "900", "500"); //open popup
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="edit-user">
     <a href="example.com/nokidding">No kidding</a>
 </div>

1 Comment

I feel very happy about that
0

This should work :)

$(document).ready(function(e){
$(".link").attr('onclick','Popup("address_edit","Edit","900","500");');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a class="link" href="example.com/nokidding">No kidding</a>

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.