1

I have an array that I want to pass to a javascript onclick function. I send that array in the onclick event of a button. The onclick function return me [object Object]. Is there a different way to do call that function with the array?

Here My Code:

var data_type= $.parseJSON(data_playtype[1]);//{"1":"apple","2":"orange","23":"berry","8":"grape","9":"mango"}


str1.push('<td><a href="javascript:void(0)" onClick="showABC(\''+data_type+'\')">'Data'</td>');



function showABC(fruit){

    $.each(fruit,function(pid,pt){
        alert(pt);
    });

}
4
  • 2
    Don't use HTML and inline event handlers. Use jQuery to build the DOM elements and to bind the event handler, so you can make use of closures. I highly recommend to read the jQuery tutorial: learn.jquery.com/using-jquery-core/manipulating-elements/…, learn.jquery.com/events/event-basics Commented Jun 22, 2015 at 2:11
  • pt is an object, so when you do alert(pt), it's stringifying the object to [object Object]. Try to access a specific key of that object. Commented Jun 22, 2015 at 2:11
  • @JoshBeam: The string conversion already happens earlier when the HTML is built. Commented Jun 22, 2015 at 2:12
  • @FelixKling, oh yeah, you're right. Talking about in the string'ed onClick, yes? Commented Jun 22, 2015 at 2:13

2 Answers 2

4

I have an array that I want to pass to a javascript onclick function. I send that array in the onclick event of a button. The onclick function return me [object Object]. Is there a different way to do call that function with the array?

Note, data_type not appear to be Array at js ; data_type appear to be Object


Try utilizing .on() , calling showABC() within click event handler with data_type as parameter.

var data_playtype = [];

data_playtype[1] = JSON.stringify({
  "1": "apple",
  "2": "orange",
  "23": "berry",
  "8": "grape",
  "9": "mango"
});

var data_type = $.parseJSON(data_playtype[1]);
//{"1":"apple","2":"orange","23":"berry","8":"grape","9":"mango"}

var str1 = [];
str1.push("<td><a href=javascript:void(0)>Data</a></td>");

function showABC(fruit) {

  $.each(fruit, function(pid, pt) {
    alert(pt);
  });

};

$("table tbody").append(str1).find("a")
  .on("click", function() {
    showABC(data_type)
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table>
  <tbody></tbody>
</table>

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

Comments

0

DEMO

Using inline JS is not a good idea. I would use a data attribute to hold the data as follows:

var data_type = .....;
var link = $('<a/>').data('data_type', data_type).html('Data');
str1.push( $('<td/>').html( link ) );

//event handler
link.on('click', function(e) {
    e.preventDefault();
    var data_type = $(this).data('data_type');
    .....
});

Ref: https://api.jquery.com/data/

.data( key, value )

key

Type: String A string naming the piece of data to set.

value

Type: Anything The new data value; this can be any Javascript type except undefined.

4 Comments

Does jQuery automatically convert objects to JSON when passed to data?
@FelixKling, what exactly do you mean? What's your concern?
Never mind, I was confused by "data attribute". Forgot that jQuery has its own data store.
But you do bring up a good point. No actual data attribute is added to the element.

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.