How do you pass the variable value inside the $ () prototype method?
function insertChildDiv(parentId)
{
var ChildComment = new Element('div', {'class':'padding'});
ChildComment.update('testing');
$().insert(ChildComment);
}
I need parentId value inside $ () string in string
$().insert(ChildComment);
I've already tried
$('parentId').insert(ChildComment);
but it evaluates parentId as a string and looks for a div with element id "parentId" so the error will be $ ("parentId") is null
also tried
$(parentId).insert(ChildComment);
but its error return "$ (parentId) .insert is not a function
btw script is called by this html
<a href="#" onclick="insertChildDiv(123456)" id="123456more">Replies and more</a>
$ () takes a string or DOM element as a parameter
Edited to reflect changes in the original Q.
$ (parentId) will work as long as the id is valid.
the ID passed to the function is "123456" and the ID you are looking for is "123456more". Also, what you are passing is an integer (123456), not a string. Anyway, since they don't match, the prototype doesn't find it. Also, the id must not start with a number: IE8 will fail as the w3c spec says the id attribute cannot start with a number (if I remember correctly)
Try changing your html to
<a href="#" onclick="insertChildDiv('more123456')" id="more123456">Replies and more</a>
and it should work
a source to share
THIS . "this" refers to the element itself, so pass the element's reference to your function, not a string.
<div onclick="insertChildDiv(this)">###</a>
<script type='text/javascript'>
function insertChildDiv(element) {
$(element).insert(new Element('div',{'class':'padding'}).update('testing'))
}
// or if you want to add an ID:
function insertChildDiv(element) {
$(element).insert(new Element('div',{'class':'padding',id:'more'+element.id}).update('testing'))
}
</script>
a source to share
the parentId value is the id of the div where this one is <a href="#" onclick="insertChildDiv('more123456')" id="more123456">Replies and more</a>
. I passed this parentId value in the insertChildDiv () function so that the inserted child div will be nested within that parent div. also the tag id id="more123456"
will be used to change the href value to a value that will hide the inserted child div
thanks, I will rename the ids of the elements so that the first letters are in letters.
if i remove single qoutes that wrap parentId inside $ () ex. $(parentId).insert(ChildComment);
returned error:
$(parentId).insert is not a function
if i end parentId in single quotes, prototype doesn't find div with error id parentId, would be $("parentId") is null
a source to share
Wow, thanks everyone! now it works. in the interest of others who might run into the same scenario, here's what I think: SHOULD NOT USE NUMBERS ONLY as an identifier for ELEMENT (even if the word "id" suggests it should be a number.) AND YES if you really need number on this id, concat even one letter at the beginning of id ex. now id im using id = "parent123456"
a source to share