0

Im trying to load article from json (its works) and add it link to fucntion, but when I add the a href tag with onclick function (for example - alert), the function doesn't work.

what am I missing?

$.getJSON(categoryAddr,function(data){
    json=data;
    jQuery(function ($) {
    $.each(json, function (i, value) {
        var list ="<a href='#' onclick='alert('hey!');'>"
        +"<h1>" + value.title + "</h1>" 
        + "<img src='" + value.image + "' alt=''/>"
        +"<h2>" + value.excerpt + "</h2></a>";
        $('.hold').append(list);
        });

    });

}); 
9
  • look in your developer tools console for errors Commented Feb 4, 2016 at 10:28
  • check for errors when click on the link you are generating in the each loop Commented Feb 4, 2016 at 10:31
  • I mean when you click - there will be errors I'm almost positive Commented Feb 4, 2016 at 10:33
  • nothing happen when I click Commented Feb 4, 2016 at 10:33
  • nothing happen when I click that is given since there is a unescaped quote. Can you check for errors in console (Browser console) when you click? Commented Feb 4, 2016 at 10:35

4 Answers 4

3

you need to escapq quotes inside the alert

change this line

var list ="<a href='#' onclick='alert('hey!');'>"

to

var list ="<a href='#' onclick='alert(\'hey!\');'>"
Sign up to request clarification or add additional context in comments.

2 Comments

That is why i don't like inline handlers. better to go for delegated events.
@Jai that is true, but OP is asking What am i missing :)
1

Use escaped double quotes around your JS

var list ="<a href='#' onclick=\"alert('hey!');\">"

Comments

0

Define onclick event outside instead of defining it inline as shown below

 jQuery(function ($) {
     var json={title:'title1'};
    $.each(json, function (i, value) {   
        var list ="<a href='#' onclick=\"alert('hey!');\">"
        +"<h1>" + value + "</h1>" ;
        $('.hold').append(list);
        });      
    });

Fiidle : https://jsfiddle.net/9qes9u8u/

Updated Fiddle based upon @rzr answer

https://jsfiddle.net/t8o56g28/

Comments

0

I would go for delegated events:

var list ="<a href='#' class='link'>"

Now you can use this script, and you don't need to have a nested doc ready block:

$.getJSON(categoryAddr,function(data){
    json=data;
    $.each(json, function (i, value) {
        var list ="<a href='#' data-id='" + value.id +"' class='link'>"
        +"<h1>" + value.title + "</h1>" 
        + "<img src='" + value.image + "' alt=''/>"
        +"<h2>" + value.excerpt + "</h2></a>";
        $('.hold').append(list);
    });  
});   

This is delegated event on newly created anchor:

 $('.hold').on('click', '.link', function(){
    alert($(this).data("id"));
 });

2 Comments

tnx! can I assign the alert var from json? for ex. alert(value.title)
Absolutely! But other way round like you can use data-* attribute to store it and alert it. Updating.

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.