Can I make an address link activate a javascript? - javascript

I currently have this code:
<input id="doCheck" type="button" onclick="doCheck('a'); return false;" />
But I want to convert this to use an address link and not an input because I have created my own class btn and I want to use that.
<a class="btn" href="#(Model.NextUrl)" title="Go to next">>></a>
Is it possible to do this. To make the clicking on the link activate Javascript. Also can I stop Google from getting interested in the address?

sure, you can add the onclick to the anchor:
<a class="btn" href="#(Model.NextUrl)" title="Go to next" onclick="doCheck('a'); return false;">>></a>

Link text
the onclick can be used here
rel="nofollow" will tell google jnot to 'click' the link

Yes, you can. The onclick attribute may be used with most elements.
<a class="btn" href="#(Model.NextUrl)" onclick="doCheck('a'); return false;"
title="Go to next">>></a>

By adding the onclick to your hyperlink, you avoid Google from tracking the address of the link since the JavaScript is not contained in the href. Either way, Google doesn't track JavaScript in hrefs anyway.
<a class="btn" href="#(Model.NextUrl)" title="Go to next"
onclick="doCheck('a'); return false;">>></a>
Alternatively, you can add a rel tag to any anchor that tells Google not to track the address of the hyperlink:
<a class="btn" href="#(Model.NextUrl)" title="Go to next"
onclick="doCheck('a'); return false;" ref="nofollow">>></a>

Related

Link / anchor has only the effect of adding to the context menu

fiddle
<a href="//google.com">
<button onClick="event.stopPropagation();">
click
</button>
</a>
What Im after:
I want "copy link location" in my context menu.
Besides that I want the <a> to be NOOP (no operation).
=> I have the benefit of "copy link location" and at the same time no redirect-stuff is happening and the normal button functionality is in operation.
Its easier than I thought.
<button onClick="event.preventDefault();"> ...
preventDefault

How to toggle one of many links with icons using JavaScript

I have several dynamically created links which rendered as buttons and the buttons texts are replaced with icons. I need to toggle one of the link button icons when clicked. The method that I am using is not working out. See code below: I do not want to use JQuery at this time unless it’s within a function.
<a class="button" onclick="command('removeFormat');" title="Remove Format"><i class="fas fa-eraser"></i></a>
<a class="button" onclick="command('fullScreen');" title="Full Screen"><i class="fas fa-expand"></i></a>
<a class="button" onclick="doToggleView();" title="Source"><i class="fa fa-code"></i></a>
<a class="button" onclick="submitForm();" title="Save"><i class="far fa-save"></i></a>
//JS
function command(cmd){
if(cmd == 'fullScreen'){
$(".fa-expand").toggleClass('fa-expand fa-compress');
}else{
$(".fa-compress").toggleClass('fa-compress fa-expand');
}
}
I also try using the following codes:
$("i").toggleClass('fa-compress fa-expand');
$("a .button").find("i").toggleClass('fa-expand fa-compress');
This is the fix to resolve the issue.
function command(cmd){
$('i.fas').toggleClass('fa-expand fa-compress');
}

Html and javascript: how to switch download link to button

I have a export to excel link on my page like:
<a download="queryResults.csv" href="#" onclick="return ExcellentExport.csv(this, 'datatable');">Export to CSV</a>
I would like to present it as a button, I tried something like:
<button download="queryResults.csv" href="#" onclick="return ExcellentExport.csv(this, 'datatable');">Export to CSV</button>
Hoe should I get it to work? Thanks.
<a download="queryResults.csv"
onclick="return ExcellentExport.csv(this, 'datatable');" href="#">
<button>Export to CSV</button>
</a>
The button element has no href attribute. Instead you have to provide a type attribute, which has to be type="button". See the specs here: HTML button tag

Using href attribute for button element

I need that use href="" attribute for button
like this <button href="link">click me</button> Instead of <a href="link">
that's wrong .
I think that I've to try this code
<button onClick="FN('MY Link For Example www.stackoverflow.com')" >click me</button>
<script>
function FN('URL'){
--> Go to MY Link For Example www.stackoverflow.com
}
<script>
So what do u guys suggest I should do ?
Try this. This will navigate to the link on click.
<button onclick="window.location.href='http://www.stackoverflow.com'">click me</button>
Why you don't use <a type="button" href=""></a>
Or if you really want a button, so <button onclick="window.location.href=""></button>
I think this will solve your problem
<a href="http://www.stackoverflow.com/">
<button>click me</button>
</a>
href attribute can not be used on button tag , if you want button like appearance for your a tag you can use add style to your a tag and make it look like button , add bootstrap reference then add button class to your anchor tag.
Custom Bootstrap Button , but if you still want to use button tag then adding onclick event is better option

ASP.NET and jQuery remove button permanently

I have a link with button behavior (bootstrap built-in) that i need to remove once it's clicked.
<a class="btn btn-success" type="button" href="#default">Home Page</a>
Currently i am using jQuery to remove the button e.g.
var btn = $('.btn');
btn.remove();
Actually this works just fine except that if you refresh the page the button as you guess it present on the page again. I want to remove it permanently though not sure if it's doable on the client side.
The project is an ASP.NET one-page web app.
Thank you
event.preventDefault() should help in this:
<a class="btn btn-success" type="button" href="#default">Home Page</a>
$('.btn').click(function(event){
event.preventDefault();
$(this).remove();
});

Categories