asked    Bill     2018-07-24       javascript       151 view        2 Answers

[SOLVED] Unbind preventDefault after run a function

I have a <a> and I want when I click on it, respectively, first run a function then <a> URL bring me to another page.

HTML:

<a class="goTo" href=""> Projects </a>
<a class="goTo" href=""> About</a>

JS

$('.goTo').click(function(e) {
  e.preventDefault()
  return x()
})

  2 Answers  

        answered    William     2018-07-24      

Just remove e.preventDefault().

Event handlers run before the default action (which, in the case of a link, is to cause the browser to navigate to another page).

$('.goTo').click(function(e) {
  return x()
})

function x() {
  alert("Function x");
}
<script src=""></script>
<a class="goTo" href=""> Example </a>


        answered    Kitty     2018-07-24      

HTML:

<a class="goTo" href="" onClick="doSomething(this)"> Projects</a>
<a class="goTo" href="" onClick="doSomething(this)"> About</a>

Javascript:

function doSomething(element){
  someFunction();
  //use element.href to get the href attribute of the element
  location.href = element.href;
}

jQuery (alternative):

$('.goTo').click(function(e){
  someFunction();
  location.href = $(this).attr('href');
});

JSFiddle:





Your Answer





 2018-07-24         Flora

D-link Modem & Linksys Router Wrtg54

I have recently brought a static ip address from my isp and i have a old computer to which i want to make my website live on internet i have read couple of forums and done research from it and nowhere i am to follow that. I have a D-link modem and linksys router wrtg54 my isp have set up the static ip on d-link modem now i am confused what to do with the static ip as per my research many static ip are written on networks ip address,subnet,gateway and dns. I dont know how to set this up.I had setup a static ip on my server computer which is 192.168.192.103 now i dont know wh...
 server                     1 answers                     9 view
 2018-07-24         Nancy

Angular 5 Router Module No provider for ChildrenOutletContexts! error

I am working on the part of routing in my Angular 5 project.I did the following:I created a new routes module called routes.module.ts:import { RouterModule, Routes } from '@angular/router';import { UdemyComponent } from './udemy/udemy.component';const appRoutes: Routes = [ {path: 'udemy', component: UdemyComponent}];@NgModule({ imports: [ RouterModule.forRoot(appRoutes, { enableTracing: true})//<--Debugging purposes only) ]});export class RouteModule {}In the app.module.ts:import { RouterModule, Routes } from '@angular/router';import { RouteModule } from ...
 angular                     2 answers                     13 view
 2018-07-24         Anastasia

Multiple routers vs single router in BackboneJs

All examples on Backbone I've seen use one router for the whole application, but wouldn't it make sense to have a router for each single part of your app (header, footer, stage, sidebar)? Has anyone built apps with more than one router and what are your experiences?Let's think about a complex app with nested views: Wouldn't it be better when a view has its own router that handles the display of the subviews, than having one big router that has to inform the main view to change its subviews?The background of this question: I've see a lot of parallels of the router in backbon...
 javascript                     3 answers                     12 view