1

i have a div area in which i added some json input from an ajax success function.

function rowClicked(term){
$.ajax({
    url: "/student/transcript_detail",
    data:{term:term},
    on:"/student/transcript_detail",
    method:"POST",
    success: function(responseJson) {
        $('#course-brief-row').empty();
        $('#nomre_term_body').empty();
        $.each(responseJson, function(index,termDetail){
            $("#course-brief-row").append("<a class='style-item' onclick='courseClicked(${termDetail.coirseId},${term})'>" +termDetail.coursename+"<br>"+
                                            termDetail.grade+"<br>"+
                                            termDetail.stat+"<br>"+
                                            "<p><br>&nbsp;</p>"+
                "</a>");""
        });
    },
    error: function () {
        alert("ERROR");
    }

});

then i want each added <a> tag, to send my json parameter as an input for their onclick function calling,

function courseClicked(coid,term){
    //do sth
}

now i want to know how should i send this argumats? In other words how should i write this part:

onclick='courseClicked(${termDetail.coirseId},${term})

2 Answers2

1

This should suffice I suppose-

onclick='courseClicked("+termDetail.coirseId+","+term+")'

Its a normal javascript variable that you are passing on anyways.

Try the fiddle - https://jsfiddle.net/607notn6/.

Rajeev Ranjan
  • 3,588
  • 6
  • 28
  • 52
  • Try the fiddle I have added. It works just fine. This is the exact similar of what you are looking for perhaps. – Rajeev Ranjan Jun 24 '17 at 12:58
  • One of my function argument's is a JSON object directly fetched from server. Any chance that this prevents my function from working? Maybe there must be a different syntax for JSON object. – Pegah Kiaei Jun 24 '17 at 13:05
  • You can do a console.log() to check if the json object is populated and you are accessing it correctly. You need to add the statement `console.log(termDetail.coirseId+","+term)` or `alert(termDetail.coirseId+","+term)`just before `$("#course-brief-row").append...` statement. – Rajeev Ranjan Jun 24 '17 at 13:08
  • try updating your function declaration to a function expression as `courseClicked = function(coid,term){..}`. – Rajeev Ranjan Jun 24 '17 at 13:20
  • the json object is populating correctly.but i don't know to updating my function with such decleration:( – Pegah Kiaei Jun 24 '17 at 13:24
  • Just replace `function courseClicked(coid,term){` with `courseClicked = function(coid,term){` wherever you have defined your function courseClicked. https://javascriptweblog.wordpress.com/2010/07/06/function-declarations-vs-function-expressions/ – Rajeev Ranjan Jun 24 '17 at 13:26
  • i tried it but i still get no response.more, i found that in my IDE it has been mentioned that attribute `+termDetail.coirseId+` is not allowed. – Pegah Kiaei Jun 24 '17 at 14:31
  • Were the console.log/alert working for you? Could you please share the complete javascript code here or in fiddle? – Rajeev Ranjan Jun 24 '17 at 14:40
  • it worked!!!!:)hura! thank you alot, i've just checked some other permutations:)) to see if it works or not and it did. ps:i used this : `onclick='courseClicked("+termDetail.coirseId+","+term+")'` syntax and i defined a variable for ``. – Pegah Kiaei Jun 24 '17 at 14:55
0

This is the example now you need a little more work.

var var_text = "ola";
var a = document.createElement('a');
var linkText = document.createTextNode("my title text");
a.appendChild(linkText);
a.title = "my title text";
a.setAttribute('onclick', 'fuction_test(var_text)');
document.body.appendChild(a);
function fuction_test(var_text1){
  alert(var_text1);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Any doubt, just ask for my help. I will try to help you.

Jose Marques
  • 748
  • 1
  • 6
  • 22