...

View Full Version : create element("a") problem



anthor
08-25-2011, 01:54 PM
var newlink = document.createElement("a");
newlink.setAttribute("class", "mhs uiButton");
newlink.setAttribute("role", "button");
newlink.setAttribute("href", "http://www.facebook.com/#");
newlink.setAttribute("onlick","FriendBrowserCheckboxController.makeFriendRequest(this, '+javaedit.text+'); return false;");
alert(newlink.classname);

why alert newlink.classname return as undefined ? i have set class = mhs uiButton already,should be return mhs uiButton?

DaveyErwin
08-25-2011, 02:35 PM
Your code is OK in firefox but not ie

checkout this link

http://www.quirksmode.org/bugreports/archives/2005/03/setAttribute_does_not_work_in_IE_when_used_with_th.html

alert(newlink.classname);

should be

alert(newlink.className);

anthor
08-25-2011, 08:24 PM
<DIV class=UIImageBlock_Ext>
<DIV id=friend_browser_unit_100002258463790 class="friendBrowserUnit ">
<INPUT class=friendBrowserID value=100002258463790 type=hidden name=friend_browser_id[] autocomplete="off">
<DIV id=friendBrowserActionId_100002258463790 class="friendBrowserAddAsFriend stat_elem">
<A class="mhs uiButton" role=button onclick='FriendBrowserCheckboxController.makeFriendRequest(this, "100002258463790"); return false;' href=" http://www.facebook.com/#">
<I class="mrs img sp_du7a4w sx_4c9e49">
</I>
<SPAN class=uiButtonText>Add Friend</SPAN>
</A>
</DIV>
</DIV>
</DIV>

since i have create all the attributes like this:

var newlink = document.createElement("A");
newlink.setAttribute("className", "mhs uiButton");
newlink.setAttribute("role", "button");
newlink.setAttribute("href", "http://www.facebook.com/#"); '
newlink.setAttribute("onclick",function() { FriendBrowserCheckboxController.makeFriendRequest(this, "100002258463790");
return false;});
FriendBrowserCheckboxController.makeFriendRequest(newlink, "100002258463790"); alert(newlink.className);

but the function FriendBrowserCheckboxController.makeFriendRequest still can't work,what's wrong?

devnull69
08-25-2011, 08:54 PM
You should forget about setAttribute. Most of the time you can use the element properties instead. In case of "on" event handlers you even have no other choice



var newlink = document.createElement("a");
newlink.className = "mhs uiButton";
newlink.setAttribute("role", "button");
newlink.href = "http://www.facebook.com/#";
newlink.onclick = function() {
FriendBrowserCheckboxController.makeFriendRequest(this, "100002258463790");
return false;
};
FriendBrowserCheckboxController.makeFriendRequest(newlink, "100002258463790"); alert(newlink.className);



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum