1

I have the code below.

When I trigger #tempbutton from #searchbutton (with an object as a parameter), it works fine. But when I'm doing the same thing from #myid, it won't trigger.

Any ideas what I could be doing wrong? Thanks

Here's my code. Please assume getAjax is called from somewhere. It's just that I had to stripped a lot of code to make it short/readable. But that part definitely works. :)

var onclickmyid = function(event, parameters) {
    event.preventDefault();

    console.log(event.data); //works. it displays the listlength passed

    $("#tempbutton").trigger("click", [ "test" ]); // works but obviously, it won't be an object
    //$("#tempbutton").trigger("click", [{ name:"test" }]); // doesn't work although it' the same as the one below
};

var onclicktempbutton = function(event, parameters) { console.log(parameters); }

var onclicksearchbutton = function(event) {
    event.preventDefault();

    $("#tempbutton").trigger("click", [{ name:"test" }]); //works
};

$(document).on("click", "#tempbutton", onclicktempbutton);

$(document).on("click", "#searchbutton", onclicksearchbutton);

function getAjax() {
    $.ajax({
        url: my-url-here
        , cache: false
        , dataType: "json"
        , data: {}
        , success: function(data, status) {
            displayTopicList({ data:data });
        }
    });
}

function displayTopicList(parameters) {
    $.each(parameters.data, function(index, option) {
        $("#mylinks").append($("<a></a><br />").attr("id", "myid"+option.id).attr("href", "javascript:void(0)");
    });

    $(document).on("click", "[id^=myid]", { listlength:parameters.data.length }, onclickmyid);
}
mrjayviper
  • 2,258
  • 11
  • 46
  • 82
  • do you call displayTopicList(parameters)? Oh and what is this strange notation: data..length? – Jochen Schultz Apr 29 '15 at 08:21
  • yes. it's called somewhere. just not clear on the code as I have to snip a lot to make it short/readable :) and that's called a typo. thanks for picking it up. edited my original post. – mrjayviper Apr 29 '15 at 08:22
  • 1
    Try `onclicktempbutton = function() { console.log(arguments); }` – Jeremy Thille Apr 29 '15 at 08:27
  • That worked Jeremy. Any ideas what could be happening with my original code? And could you also put your post as an answer? Thanks very much – mrjayviper Apr 29 '15 at 08:37

0 Answers0