I have captured 3 values from a datalist template using JavaScript, now I need to append these values into an existing anchor which is:
<a id="link" href='nextpage.aspx?id=<%#Eval("PlateId")%>&pp= #Eval("price")%>'>
I found the way to get the anchor href from JavaScript:
<script language='javascript' type="text/javascript" >
function addLink() {
var anchor = document.getElementById("link");
anchor.href = anchor + "&qty=";}
</script>
But, I can't add the js value after "&qty=", I have tried adding the value like this:
anchor.href = anchor +"&qty=+Value+"
And with this:
anchor.href = anchor +"&qty='Value' "
I can't put it out of the quotation marks, because it won't display in the anchor.
anchor.href = anchor + "&qty="
should be
anchor.href = anchor.href + "&qty="
...otherwise you are trying to turn the link element into a string!
Meanwhile, you still need to keep Value out of the quotation marks, otherwise the final text generated will always be exactly that text: Value. If Value is a variable and is not returning the value you expect (an empty or undefined value, for example), then you need to look at the code that declares it and change that.
Dynamically change href atrribute value (or) You can
able to append the existing URL
Tips: 1 - Use the jQuery .attr() Method
$("selector").attr("href", newURL));
Tips: 2 - Use javascript element selector document.getElementById
example:
TEST URL
var redirect_url = document.getElementById('test');
redirect_url.href = "new url";
(or)
document.getElementById('test').href = "newURL";
(or)
using setAttribute() method *Note:broken in IE
redirect_url.setAttribute("href", "new url");
Related
I have these two variables:
var a_link = "someLink.com";
var a_text = "Some url text";
I'm trying to insert these two variables into an anchor tag. Like this:
a_text
Here's what I tried so far:
<a href="javascript:window.open(a_link);">
How can I change the a_link and the a_text to the corresponding variables?
What you're trying to do can be achieved by DOM manipulation.
Basicly: you have an anchor element <a> with two variables: the href and the inner HTML. You just need to find the anchor element and change these properties to the corresponding variables.
var a_link = "someLink.com";
var a_text = "Some url text";
var anchor = document.getElementsByTagName("a")[0];
anchor.href = a_link;
anchor.innerHTML = a_text;
The [0] you see after the document.getElementsByTagName("a") is there to state that we're looking for the first anchor element. If you were trying to change, say, the third <a> you'd use [2].
You can read more about DOM Manipulation here.
There is a function called "dictionary link" in Anki as the manual explains:
Dictionary Links
You can also use field replacement to create dictionary links.
Imagine you’re studying a language and your favourite online dictionary allows you to search for text using a web URL like:
http://example.com/search?q=myword
You could add an automatic link by doing the following in your template:
{{myword}}
check in dictionary
The template above would allow you to search for each note’s expression by clicking on the link while reviewing.
I am now learning HTML + CSS + Javascript from scratch, I'd like to add a similar tool in my own practice website.
I want to copy the text content (the word that I want to check in the dictionary) of an element, add it to the end of the url. When I click the link the corresponding dictionary page will show up.
For example:
<span id="search">entry</span>
copy "entry" and add it to the end of
<a id="dictionary" href="http://example.com/search?q=">link</a>
Since I am a complete beginner, I haven't learned jQuery or other tools yet. Is it possible to do this only by HTML and Javascript?
const search = document.getElementById("search");
const link = document.getElementById("dictionary");
link.href = `http://example.com/search?q=${search.innerText}`;
You have to assing new href property to link by getting innerText of search element
Did you try to use these apex:
<a href=`http://example.com/search?q=${myword}`>check in dictionary</a>
Otherwise you have to build the link by js script and then assign to a element:
let link = 'http://example.com/search?q=' + customParam
// Fetch the tag <a>
let hrefElement = document.getElementById('#idElement');
// Change the href param with your link
hrefElement.href = link;
Try this link and try to work around
function clicks(){
var foo = document.getElementById("dictionary").id
$("a").attr("href", "http://example.com/"+foo+"?q=")
}
document.getElementById("myLink").href = customLink will set your link to your a element
//get input element
var inputElement = document.getElementById('param-input');
// add event to listen every time a letter is introduced
inputElement.addEventListener("keyup", composeUrl);
function composeUrl() {
// get value from input
var param = inputElement.value;
// compose url
var customLink = "http://example.com/search?q=" + param
//set href
document.getElementById("myLink").href = customLink
//get href
var result = document.getElementById("myLink").href;
//print link in a div
document.getElementById("demoLink").innerHTML = result.toString()
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id="param-input" />
<a id="myLink" />
<div id="demoLink"> url composed </div>
I think this may have a simple answer that I'm missing. The following tag inserts a TV show name into any page on my website:
<span class="show-title"></span>
what I'm trying to do is incorporate that data dynamically into a HREF URL link.
So, let's say on the page I'm on:
produced the result: GOTHAM.
I'd like to then use that data to create this url:
https://en.wikipedia.org/wiki/GOTHAM_(TV_series)
So I'm trying stuff like:
</span>_(TV_series)"> Link
or
Link
nothing working - any help would be awesome. Thanks!
You could do something like this:
In HTML
<a class="wikipedia-link">Link</a>
And your JavaScript function:
setLink(showTitle) {
var link = document.getElementsByClassName("wikipedia-link");
link.setAttribute("href", "https://en.wikipedia.org/wiki/" + showTitle + "_(TV_series)");
}
The html you use is wrong. span shouldn't be inside tag a. No tag inside another.
If your result is in javascript variable, you can set the url using jquery.
$('a').attr('href', "https://en.wikipedia.org/wiki/" + result + "_(TV_series)");
result variable is your desired result.
Although there's better ways of going about doing this, I'm going to answer the question in the context in which you presented it:
First, give the url link a class or ID so you can easily select it with JavaScript to change the href value later. Also, don't try to nest the span tag anywhere inside the a tag. Leave it outside.
<span class="show-title">GOTHAM</span>
Link
Next, in a JavaScript file or a <script> tag, define your variables:
var showTitle, showWikipediaLink, linkElement
Then, assign value to your newly defined variables
linkElement = document.querySelector('.show-wikipedia-link');
showTitle = document.querySelector('.show-title').innerText;
showWikipediaLink = 'https://en.wikipedia.org/wiki/' + showTitle + '_(TV_series)';
Finally, use JavaScript to update the href value of the link element to the show's Wikipedia link:
linkElement.href = showWikipediaLink;
I made a mistake and forgot to use the attribute value in some code I was writing:
var link = document.getElementsByClassName("summary-title-link")[0],
ele = document.createElement("a");
ele.href = link;
and I was surprised to see that it still worked regardless.
In extension with this example below, I find it odd that I don't need to target the href attribute before using pathname? it seems to assume somehow that I want the pathname from the href attribute.
var link = document.getElementsByClassName("summary-title-link")[0].pathname;
"/test-link/1"
When you convert an anchor element to a string, you actually get the href value, or more precisely "the whole URL", and not the outerHTML as you would with most other elements, that's why it works
var href = document.getElementsByClassName("test")[0]; // DOM element
console.log(href.toString()); // gives you "http://google.com"
<a class="test" href="http://google.com">link</a>
This special behaviour for anchors is specified in the specification
HTMLHyperlinkElementUtils.toString()
Returns a USVString containing the whole URL.
It is a synonym for URLUtils.href, though it can't be used to modify the value.
I am using dojo for scripting.
I want to give the href tag which link to the local path of an excel file.
It works fine in IE ie., when I click on the link it asks for open/save/cancel.
But the same code isn't working for Firefox.
Is there any workaround for that?
I am writing the code, I look forward to your useful comments.
var href = dojo.place ('<"a href = /path/abc.csv"><Export></a>',dojo.body());
You have your quotes in the wrong place:
var href = dojo.place ('<Export>',dojo.body());
// Not here -------------^ ^
// Yes here ----------------------+
Your HTML syntax is invalid - it should be Export.
Probably because of the place of the quote. It should be after href
var href = dojo.place ('<a href = "/path/abc.csv">',dojo.body());
You are placed double quoate at anchor tag, it is wrong,
var href = dojo.place ('<"a href = /path/abc.csv"><Export></a>',dojo.body());
Your anchor tag href should be change as
var href = dojo.place ('<Export>',dojo.body());
Your quotes " ... " are not at correct place and syntax is not valid. it should be:
var href = dojo.place ('<Export>',dojo.body());
^
//note quotes here................^