/    Sign up×
Community /Pin to ProfileBookmark

create element ("a") problem

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?

to post a comment
JavaScript

9 Comments(s)

Copy linkTweet thisAlerts:
@DracoMerestAug 25.2011 — alert(newlink.class[COLOR="Blue"]N[/COLOR]ame);

JavaScript attributes and functions are case sensitive, mostly.
Copy linkTweet thisAlerts:
@anthorauthorAug 25.2011 — ya..it's work, but return a blank value, why? should be return "mhs uiButton"
Copy linkTweet thisAlerts:
@DracoMerestAug 25.2011 — I use FireFox and it does return the correct value.

Which browser do you use?
Copy linkTweet thisAlerts:
@anthorauthorAug 25.2011 — <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?
Copy linkTweet thisAlerts:
@Declan1991Aug 25.2011 — Problem with setAttribute and class names, and with the word class in general. Cross-browser solution is:

newlink.className = "mhs uiButton";
Copy linkTweet thisAlerts:
@svidgenAug 25.2011 — In general, it should be OK to use the more concise version that Declan1991 used:

[code=php]var newlink = document.createElement("a");
newlink.className = "mhs uiButton";
newlink.role = "button";
newlink.href = "http://www.facebook.com/#";
newlink.onlick = "FriendBrowserCheckboxController.makeFriendRequest(this, '+javaedit.text+'); return false;";

alert(newlink.className);[/code]
Copy linkTweet thisAlerts:
@zoltankisAug 26.2011 — try this variant:
<i>
</i>var secondArg=javaedit.text;
if (0==1){secondArg='+javaedit.text+'};
newlink.on[COLOR=Red]c[/COLOR]lick =function(){FriendBrowserCheckboxController.makeFriendRequest( this, secondArg ); };
Copy linkTweet thisAlerts:
@Declan1991Aug 26.2011 — And I forgot to add, onclick isn't an attribute, so either use element.onclick = function () {} or .addEventListener and .addEvent. setAttribute with events could work, but it shouldn't and is incorrect.
Copy linkTweet thisAlerts:
@svidgenAug 26.2011 — And I forgot to add, onclick isn't an attribute, so either use element.onclick = function () {} or .addEventListener and .addEvent. setAttribute with events could work, but it shouldn't and is incorrect.[/QUOTE]

Touche!
×

Success!

Help @anthor spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.3,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...