1

I have the below HTML file . I am trying to pass the item object to the javascript function : linkClickFunc so that i can process it further.

But i am unable to do it with the below syntax . Can someone please guide me as to what am i doing wrong .

    <div class="container">
  <h2>CodeConfig Keys</h2>
  <br>
  <div id="key-buttons">
  </div>
</div>

<script type="text/javascript">
function linkClickFunc(item)
{
  console.log(item);
}
</script>

<script>
$(document).ready(function(){
    CodeConfigkeys = {"ENV": [{"NAME": "Subhayan", "Values": [{"AGE": 33, "SEX": "Male"}]}, {"NAME": "Mary", "Values": [{"AGE": 29, "SEX": "Female"}]}], "DB_PARAMS": [{"NAME": "SQL_CONNECTIONS_DB", "Values": "templates"}, {"NAME": "SQL_CONNECTIONS_COLLECTION", "Values": "dbtemplates"}]};
    var html_str = "";
    $.each( CodeConfigkeys, function( key, value ){
    html_str = html_str + "<div class=\"btn-group dropdown\">";
    html_str = html_str + "<button type=\"button\" class=\"btn btn-primary dropdown-toggle btn-primary-spacing\" data-toggle=\"dropdown\" id=\"" + "CodeConfigMenus" + "\">" + key;
    html_str = html_str + "</span><span class=\"sr-only\">Toggle Dropdown</span></button>";
    html_str = html_str + "<div class=\"dropdown-menu\">";
    value.forEach(function(item, index, array) {
    console.log(item);
    console.log (typeof item);
    html_str = html_str + "<li><a onclick=\"linkClickFunc($(" + "'" + item + "'" + "));\">" + item["NAME"] + "</a></li>";
    });
    html_str = html_str + "</div></div>";
    });
    console.log(html_str);
    $("#key-buttons").html(html_str);

});
</script>

I have searched for possible solutions on the internet but could not hit upon the right one.

Thanks in advance.

5
  • Store item in the data() on the element, then bind an unobtrusive event handler to it and read that data attribute back out. Commented May 31, 2018 at 9:50
  • I edited the question to give the source code. But i am not sure i followed your suggestion Commented May 31, 2018 at 9:55
  • If item is an object, the HTML output would be <a onclick="linkClickFunc([object Object])"></a>. You need to add an event listener for the click event. Commented May 31, 2018 at 9:56
  • Yes that is exactly what the error is . Forgive my lack of knowledge in this subject but isn't the function an click event listener ? Commented May 31, 2018 at 9:58
  • I meant programmatically, i.e. link.addEventListener('click', yourFunction). Commented May 31, 2018 at 10:03

2 Answers 2

1

Just change your line

html_str = html_str + "<li><a onclick=\"linkClickFunc($(" + "'" + item + "'" + "));\">" + item["NAME"] + "</a></li>";

with

html_str = html_str + "<li><a onclick='linkClickFunc("+JSON.stringify(item)+")'>" + item["NAME"] + "</a></li>";
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much . Works perfectly.
That is brilliant!
1

Use JSON.stringify() to pass the object as a string. Of course you need to escape the double quote to be recognize as real double quotes. JSON.stringify(item)

html_str = html_str + "<li><a onclick=\"linkClickFunc($(" + "'" + JSON.stringify(item) + "'" + "));\">" + item["NAME"] + "</a></li>";

Then for the function, parse the JSON object back to string. JSON.parse(item)

function linkClickFunc(item)
{
  var obj = JSON.parse(item);
  console.log(item);
}

I have not tested the code but you should handle double quote for string conversion and parsing.

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.