How to call onclick and a href in the same time?

  • (2 Pages)
  • +
  • 1
  • 2

26 Replies - 10637 Views - Last Post: 13 July 2009 - 11:37 PM

#1 livium  Icon User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 519
  • Joined: 21-December 08

How to call onclick and a href in the same time?

Posted 11 July 2009 - 05:46 AM

i have this html:

<div id="apDiv36" onclick="inc_dwn('.$id_project.')" ><a href="'.$cale.'" onclick="inc_dwn('.$id_project.')"; return false;><img src="download.jpg" border="0" > </img> </a> </div>




the inc_down function is part of a AJAX implementation. I want that this function be be executed when i click the div and in the same time the a href to work. In the code above the onclick does not work, only a href works. I have searched the net and the code above is the best i've found but it does not work.

Is This A Good Question/Topic? 0
  • +

Replies To: How to call onclick and a href in the same time?

#2 JBrace1990  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 110
  • View blog
  • Posts: 760
  • Joined: 09-March 08

Re: How to call onclick and a href in the same time?

Posted 11 July 2009 - 08:07 AM

change the onclick to a custom function, then make the function do both.
Was This Post Helpful? 0
  • +
  • -

#3 livium  Icon User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 519
  • Joined: 21-December 08

Re: How to call onclick and a href in the same time?

Posted 11 July 2009 - 08:37 AM

View PostJBrace1990, on 11 Jul, 2009 - 07:07 AM, said:

change the onclick to a custom function, then make the function do both.


i think i understand you but is there another simpler way?
Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2983
  • Posts: 10,313
  • Joined: 08-August 08

Re: How to call onclick and a href in the same time?

Posted 11 July 2009 - 07:29 PM

You say it's html, but you have a php variable:
$id_project
Is this a php file, or does the code look like that when you view source from a browser?
Was This Post Helpful? 0
  • +
  • -

#5 livium  Icon User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 519
  • Joined: 21-December 08

Re: How to call onclick and a href in the same time?

Posted 12 July 2009 - 01:36 AM

View PostCTphpnwb, on 11 Jul, 2009 - 06:29 PM, said:

You say it's html, but you have a php variable:
$id_project
Is this a php file, or does the code look like that when you view source from a browser?




it's a php who echoes a html :

  echo  '<div id="apDiv36" onclick="inc_dwn('.$id_project.')" ><a href="'.$cale.'" onclick="inc_dwn('.$id_project.')"; return true;><img src="download.jpg" border="0" > </img> </a> </div>';


Was This Post Helpful? 0
  • +
  • -

#6 JBrace1990  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 110
  • View blog
  • Posts: 760
  • Joined: 09-March 08

Re: How to call onclick and a href in the same time?

Posted 12 July 2009 - 08:02 AM

What is an example of where the link goes to? Does it go to another page?
Was This Post Helpful? 0
  • +
  • -

#7 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2983
  • Posts: 10,313
  • Joined: 08-August 08

Re: How to call onclick and a href in the same time?

Posted 12 July 2009 - 08:17 AM

View Postlivium, on 12 Jul, 2009 - 04:36 AM, said:

it's a php who echoes a html :

  echo  '<div id="apDiv36" onclick="inc_dwn('.$id_project.')" ><a href="'.$cale.'" onclick="inc_dwn('.$id_project.')"; return true;><img src="download.jpg" border="0" > </img> </a> </div>';


You need to provide more information:
What is $id_project?
What does the html look like from the browser source?
What does the javascript function inc_dwn look like? (In fact, if it weren't for $id_project, this would belong in the javascript forum. I'm not certain that it doesn't anyway.)

This post has been edited by CTphpnwb: 12 July 2009 - 08:18 AM

Was This Post Helpful? 0
  • +
  • -

#8 livium  Icon User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 519
  • Joined: 21-December 08

Re: How to call onclick and a href in the same time?

Posted 12 July 2009 - 08:58 AM

Well, the problem i had is that when i clicked this div only inc_dwn worked and a href was ignored. All i'm asking is how can I make so that onclick is executed and then a href. I doesn't matter what $id_project is. Consider it a number.

This post has been edited by livium: 12 July 2009 - 08:58 AM

Was This Post Helpful? 0
  • +
  • -

#9 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2983
  • Posts: 10,313
  • Joined: 08-August 08

Re: How to call onclick and a href in the same time?

Posted 12 July 2009 - 09:28 AM

In that case this belongs in Javascript.
Was This Post Helpful? 0
  • +
  • -

#10 SoLi  Icon User is offline

  • andydust.com

Reputation: 41
  • View blog
  • Posts: 1,438
  • Joined: 27-January 02

Re: How to call onclick and a href in the same time?

Posted 12 July 2009 - 09:51 AM

To be clear: you want an ajax request to occur (and presumably actually succeed) and then for the browser to go ahead and locate to the link specified in the anchor tag?

Sorry if that is not correct and I misunderstand you.

If it is correct; however, may I ask exactly what the purpose of it is. If the browser is going to make a normal http request as soon as the server responds to the ajax request, why not just compound whatever happens in both requests into the normal http one?

if .$cale is a hash reference to an id or named element on the same page, then I guess maybe it makes sense :)

This post has been edited by SoLi: 12 July 2009 - 10:12 AM

Was This Post Helpful? 0
  • +
  • -

#11 livium  Icon User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 519
  • Joined: 21-December 08

Re: How to call onclick and a href in the same time?

Posted 12 July 2009 - 11:06 AM

View PostSoLi, on 12 Jul, 2009 - 08:51 AM, said:

To be clear: you want an ajax request to occur (and presumably actually succeed) and then for the browser to go ahead and locate to the link specified in the anchor tag?

Sorry if that is not correct and I misunderstand you.

If it is correct; however, may I ask exactly what the purpose of it is. If the browser is going to make a normal http request as soon as the server responds to the ajax request, why not just compound whatever happens in both requests into the normal http one?

if .$cale is a hash reference to an id or named element on the same page, then I guess maybe it makes sense :)


Hello. Thanks for responding. That is exactly what i want. The div contains a picture of a button named "download". When i click the image (or div it doesn't matter) i want the Ajax to work (it increments a value in my database wich tells me how many times that file was downloaded and in the same time it puts me in a table cell that value) and of course, if i click it i want that file to be downloaded (that's why i have put the a href ancor, where $cale is the name of the file).

So..., what you said is that i can echo that "<a href=$cale...." from the ajax related php file? That i don't think it will work? But if I put also a header in my ajax php file, like so: header("location:$cale") will it work?
Was This Post Helpful? 0
  • +
  • -

#12 SoLi  Icon User is offline

  • andydust.com

Reputation: 41
  • View blog
  • Posts: 1,438
  • Joined: 27-January 02

Re: How to call onclick and a href in the same time?

Posted 12 July 2009 - 11:17 AM

So you are using ajax because it's important that the current page the user dynamically updates the displayed download count, without the user having to refresh the page?

Currently the reason the browser is not calling the location as stated in the href attribute is because of the "return false;" you have added at the end of the onclick attribute, which cancels the default behavior of the element.

Also there is no need for the onclick attribute on the wrapping div so get rid of that to make things a bit clearer.

If you remove the return false part it may work. Off the top of my head I can't remember whether or not the browser will interrupt the ajax request upon subsequently receiving new document location request; try it out and see :P

Let me know how it goes.

This post has been edited by SoLi: 12 July 2009 - 11:18 AM

Was This Post Helpful? 0
  • +
  • -

#13 livium  Icon User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 519
  • Joined: 21-December 08

Re: How to call onclick and a href in the same time?

Posted 12 July 2009 - 11:40 AM

That it's exactly what i want.

I have removed the return false statement but the problem is the same. What do you mean by removing the onclick? whitout that then the ajax is zero. Am i missing something?

I didn't tried the header thing. I will announce you when i will.

This post has been edited by livium: 12 July 2009 - 11:43 AM

Was This Post Helpful? 0
  • +
  • -

#14 livium  Icon User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 519
  • Joined: 21-December 08

Re: How to call onclick and a href in the same time?

Posted 12 July 2009 - 11:46 AM

Oh, sorry now i see that i had 2 onclick. i've deleted one but the problem is the same.
Was This Post Helpful? 0
  • +
  • -

#15 SoLi  Icon User is offline

  • andydust.com

Reputation: 41
  • View blog
  • Posts: 1,438
  • Joined: 27-January 02

Re: How to call onclick and a href in the same time?

Posted 12 July 2009 - 11:56 AM

What precisely isn't working? The function inc_dwn() or the browser opening a download dialog box for your file?

If the browser is correctly opening the download but not doing the ajax then the problem is likely to be within your javascript code.

If the browser is doing the ajax but not opening the download, then there is either a problem in your html or possibly there is another "return false;" statement somewhere within the inc_dwn() function itself, which will need to be removed.

It would help if you were to post the code for function inc_dwn().

This post has been edited by SoLi: 12 July 2009 - 11:57 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2