/    Sign up×
Community /Pin to ProfileBookmark

Return false onclick

I am calling unobtrusive JS on a link by the code below:

[code]
<a href=”?showthis=219″ onclick=”showThis(‘219’); return false;”>My Link</a>
[/code]

What I want to do is dynamically create this link and attach the onclick event. I can do that, but I don’t know the syntax to make it return false so the href doesn’t override it and refresh the page. Can somebody help me out?

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@FourCourtJesterOct 08.2008 — <i>
</i> var a = document.createElement('a');

<i> </i> a.style.textDecoration = 'none';
<i> </i> a.href = '#';
<i> </i> a.innerHTML = "My Link";
<i> </i> a.onclick = function() { showThis('219'); return false; };
<i> </i> document.appendChild(a);


Worked for me when I had to do something similar. But why would you put a location in your href when you don't want the page to go anywhere?
Copy linkTweet thisAlerts:
@ZeroKilledOct 08.2008 — But why would you put a location in your href when you don't want the page to go anywhere?[/quote]
if javascript is disabled, how you would make a link still working even having events? that's the reason for having a location (href). as [b]aj_nsc[/b] initially said, 'unobtrusive javascript'.
Copy linkTweet thisAlerts:
@aj_nscauthorOct 08.2008 — [code]
But why would you put a location in your href when you don't want the page to go anywhere?[/QUOTE]

Degradeability
Copy linkTweet thisAlerts:
@FourCourtJesterOct 08.2008 — Alrite, I concede the point. Early morning brain activity is typically low.

Did the code work?
Copy linkTweet thisAlerts:
@KorOct 08.2008 — I am calling unobtrusive JS on a link by the code below:

<i>
</i>&lt;a href="?showthis=219" onclick="showThis('219'); return false;"&gt;My Link&lt;/a&gt;


What I want to do is dynamically create this link and attach the onclick event. I can do that, but I don't know the syntax to make it return false so the href doesn't override it and refresh the page. Can somebody help me out?[/QUOTE]


There is an obvious [I]contradiction[/I] in your demand. You you want to crate dynamically the link, that means you need javascript by all means. If so, there will be no more an unobtrusive JS construction, there will be a full JS construction. Thus, the dynamic <a href="something" onclick="somefunction();return false"> construction is absolutely useless. If the user has javascript disabled, [I]no element[/I] will be present, [I]no href[/I] to be followed. If enabled, the onclick will fire the function [I]all the time[/I], thus the href is useless.

You have to decide either for a full javascript construction or for an unobtrusive JS one. If full javascript, there is no need of a link, you may simply create a SPAM, append a text node and attach the onclick event using javascript DOM methods.

Or, maybe, you want, in fact, to create dynamically [I]the event[/I], not the link... Is it so?
×

Success!

Help @aj_nsc 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.1,
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,
)...