/    Sign up×
Community /Pin to ProfileBookmark

Calling JavaScript function from <a href>

Why will this not work? When the page loads, I get an “Expected Identifier” error message. When the link is clicked, I get “‘Microsoft JScript runtime error: Object Expected’ was not handled” and it highlights “redirect(this)”:

<a href=”[url]” id=”emp” onClick=”redirect(this)”>View Multiple Employees</a>

The function looks like this:

<script language=”javascript”>

function redirect(this)
alert(this.id)

</script>

Basically what I want to do here is, on the onClick event of this link, change the action attribute of a form and submit it.

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@magusOct 14.2003 — try this:

<a href="javascript:redirect(this)" id="emp" >View Multiple Employees</a>

and adding some code to [url] on the script:

<script language="javascript">

function redirect(this)

alert(this.id) ;

location=[url]

</script>

hope this work
Copy linkTweet thisAlerts:
@CharlesOct 14.2003 — <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"

"http://www.w3.org/TR/html4/strict.dtd">

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<meta name="Content-Script-Type" content="text/javascript">

<title>Example</title>

<script type="text/javascript">

<!--

function redirect (link) {

alert (link.id);

location = link.href;

return false;

}

// -->

</script>

<a href="http://www.w3.org" id="emp" onclick="return redirect(this)">Test</a>[/font]

[font=georgia]1) Do not use the "javascript" pseudo scheme in the "href" attribute value. It will give you some unwanted results on moddern browsers.

2) You need to make sure that the page still works when there is no JavaScript.

3) You need to actually define the function.

4) The word "this" is reserved and has a special meaning.[/font]
Copy linkTweet thisAlerts:
@BananaQuaaludeauthorOct 14.2003 — Thanks to both of you-

Charles, I was able to get your method to work after putting {} around my function.

I just love switching back and forth between languages!

thanks again,

BQ
×

Success!

Help @BananaQuaalude 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.19,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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