jquery - How to pass variables to a function and return some of their properties javascript -
i have drop down menu , when select element want see list of information element. drop down , selection work since have lot of options (more showing) try make function return information element selected don't repeat code. function give me error message:"cannot read property 'top_1' of undefined" console.log(obj.top_1) works fine.
var obj = { top_1 :"bla", top_2 :"bla bla", objdetail_1 :{ top_1 :"bli", top_2 :"bli bli", name1:"aaa", name2:"aaaa" }, objdetail_2 :{ top_1 :"blo", top_2 :"bli blo", name1:"bbb", name2:"bbbbb" } }; $(document).ready(function() { $('.mystuff li ul li').click( function(event){ $(document).find('#description').css('visibility', 'visible'); var idelement = $(this).attr('id'); if (idelement == 'top_1'){ console.log(obj.top_1); ing(obj, top_1); }; function ing(element1, element2){ console.log(element1.top_1 +" "+ element1.top_2 +" "+element1.element2.top_1+" "+ element1.element2.top_2); }; }); });
thank help!
you pass element2
string , replace objdetail_1
this:
ing(obj, "objdetail_1");
then in function body access properties this:
function ing(element1, element2){ console.log(element1.top_1, element1.top_2, element1[element2].top_1, element1[element2].top_2); };
Comments
Post a Comment