i trying add method variable used following;
var method = ".wrap"; jquery('.container')+[method]+("<div>hello world</div>");
essentially should is;
jquery('.container').wrap("<div>hello world</div>");
but not work, no error, hello world div not being added in page. here original question http://goo.gl/mq2tr, thought ask again in simpler terms.
update long have single method inside variable works nice bracket notation, need use .parent().parent().wrap()
how can make work? tried removing dots error uncaught typeerror: object [object object] has no method 'parent()parent()wrap'
here how list form looks now
<select name="lu_ban_data[method]" id="lu_ban_data" /> <option value="append">append</option> <option value="prepend">prepend</option> <option value="wrap">wrap</option> <option value="parent()parent()wrap">parent</option> </select>
just write way:
var method = "wrap"; jquery('.container')[method]("<div>hello world</div>");
you can access properties of object in 2 ways. either dot notation or square bracket notation
obj.property obj['property'] var propname = "property" obj[propname]
edit
here link mdn member operators
a short explanation code does:
jquery('.container')+[method]+("<div>hello world</div>");
it addition of 3 element:
- the result set of
jquery('.container')
- an
array
containing 1 element[method]
- the
string
"<div>hello world</div>"
the result of depends on implementation this:
"[object object].wrap<div>hello world</div>" +-------------+ +---+ +--------------------+
the result looks way because javascript-engines call tostring
on elements if can't add them in way.
edit
update edited question:
element.parent().parent().wrap()
would e.g. equal to:
element['parent']()['parent']().wrap()
or
element['parent']().parent()['wrap']()
or other combination ob dot or brace notation
you want represent .parent().parent().wrap()
1 string , use access. not work way. dot notation or brace notation return property of given element. parent()
returns parent
of jquery('.container')
on returned object call parant()
, on returned object call wrap()
so (assuming last function call have arguments) need this:
function chainedfunctioncall( obj, chain, arguments) { var curr = obj; var splitchain = chain.split("."); //split 'call chain' passed strings '.' (the dot in string has nothing dot notation) //iterate on resulting array (except last 1 need pass arguments for( var i=0 ; i<splitchain.length-1 ; i++ ) { //call function given name in chain , store result current object curr = curr[splitchain[i]](); } //when reached last name in chain call function using `apply` can pass arguments got array function call, , call in context of current object. return curr[splitchain[i]].apply(curr,arguments); } var obj = $(".container"); var callchain = "parent.parent.wrap"; chainedfunctioncall( obj, callchain, ["<div>your argument pass there</div>"]);
Comments
Post a Comment