What I want is a hyperlink More when clicked on runs some javascript function and also changes to a hyperlink Less. Heres what I have which doesnt work. It runs the ajaxpage function fine but not moreToLess. I think its my use of " and ' in the javascript.
<script type="text/javascript">
function moreToLess(){
document.getElementById('tagLinks').innerHTML = '<a href=# OnClick="ajaxpage('/TagCloud?id=EDI_0009&count=20', 'tagcloud');lessToMore()" >Less</a>';
}
function lessToMore(){
document.getElementById('tagLinks').innerHTML = '<a href=# OnClick="ajaxpage('/TagCloud?id=EDI_0009&count=50', 'tagcloud');moreToLess()" >More</a>';
}
</script>
<span id=tagLinks ><a href=# OnClick="ajaxpage('/TagCloud?id=EDI_0009&count=50', 'tagcloud');moreToLess()" >More</a></span>
Yes it is, just escape your single quotes inside of the double quotes that you have in your javascript.. ala \'
<script type="text/javascript">
function moreToLess(){
document.getElementById('tagLinks').innerHTML = '<a href=# OnClick="ajaxpage(\'/TagCloud?id=EDI_0009&count=20\', \'tagcloud\');lessToMore()" >Less</a>';
}
function lessToMore(){
document.getElementById('tagLinks').innerHTML = '<a href=# OnClick="ajaxpage(\'/TagCloud?id=EDI_0009&count=50\', \'tagcloud\');moreToLess()" >More</a>';
}
</script>
<span id=tagLinks ><a href=# OnClick="ajaxpage('/TagCloud?id=EDI_0009&count=50', 'tagcloud');moreToLess()" >More</a></span>
You have to escape the ' characters inside the quote:
'<a href=# OnClick="ajaxpage(\'/TagCloud?id=EDI_0009&count=20\', \'tagcloud\');lessToMore()" >Less</a>'
I'm not sure if there's anything else wrong, but I think you need to escape your single-quotes that are within other single-quotes:
document.getElementById('tagLinks').innerHTML = '<a href=# OnClick="ajaxpage(\'/TagCloud?id=EDI_0009&count=20\', \'tagcloud\');lessToMore()" >Less</a>\';
It is indeed a quote issue, you can escape the ' character with a \ character
<script type="text/javascript">
function moreToLess(){
document.getElementById('tagLinks').innerHTML = '<a href=# OnClick="ajaxpage(\'/TagCloud?id=EDI_0009&count=20\', \'tagcloud\');lessToMore()" >Less</a>';
}
function lessToMore(){
document.getElementById('tagLinks').innerHTML = '<a href=# OnClick="ajaxpage(\'/TagCloud?id=EDI_0009&count=50\', \'tagcloud\');moreToLess()" >More</a>';
}
</script>
<span id=tagLinks ><a href=# OnClick="ajaxpage('/TagCloud?id=EDI_0009&count=50', 'tagcloud');moreToLess()
This kind of confusion is why innerHTML is not always a win. You've got JavaScript embedded in HTML embedded in a JavaScript string literal embedded in more HTML, it's no wonder you're confused.
Here's a simpler version based on toggling the link through DOM methods.
<a id="tagLinks" href="/TagCloud?id=EDI_0009&count=50">more...</a>
<script type="text/javascript">
var ismore= false;
document.getElementById('tagLinks').onclick= function() {
ajaxpage(this.href);
ismore= !ismore;
this.href= '/TagCloud?id=EDI_0009&count='+(ismore? 20 : 50);
this.firstChild.data= ismore? 'less...' : 'more...';
return false;
};
</script>
Related
I am struggling with calling a method while setting innerHTML.
I apologize if there is a straightforward answer I have overlooked, but at the moment I am stuck.
Se the code below:
"<a href='#' onclick='removeEntry('" + element.id + "')'><span class='fa fa-times'></span></a>"
You can see that there's a mess regarding the quotes.
Is there a third way to type quotes or something of the kind that can allow me to call "removeEntry(element.id)"? I need quotes around element.id in order to call removeEntry. Any suggestions on how to solve this in a different way?
You should replace
"<a href='#' onclick='removeEntry('" + element.id + "')'><span class='fa fa-times'></span></a>"
with
"<a href='#' onclick='removeEntry(\"" + element.id + "\")'><span class='fa fa-times'></span></a>"
In fact, the problem is with you current code, the a tag onclick property will be looking like onclick='removeEntry('myId')'
Note there's imbricated simple quotes, breaking your function call. Replace the id simple quotes by escaped double quotes , and it'll give you onclick='removeEntry("myId")' that is fine :)
Edit : Anyway, if you're targeting recent browser, you could try ES6 template literals, that will give you the following line :
var html = `<span class="fa fa-times"></span>`;
This helps to avoid struggling with your quotes. Note that the variable inclusion in the template literal looks like PHP do.
This:
"<a href='#' onclick='removeEntry('" + element.id + "')'><span class='fa fa-times'></span></a>"
will create an element like this:
<a href='#' onclick='removeEntry('someId')'><span class='fa fa-times'></span></a>
Which is not correct. What you need to do is to either:
Use the other quotes " (but you have to escape them):
like this:
"<a href='#' onclick='removeEntry(\"" + element.id + "\")'><span class='fa fa-times'></span></a>"
to create an element like this:
<a href='#' onclick='removeEntry("someId")'><span class='fa fa-times'></span></a>
Or Add a backslash \ to the ' quotes (you have to escape the backslashes as well):
like this:
"<a href='#' onclick='removeEntry(\\'" + element.id + "\\')'><span class='fa fa-times'></span></a>"
to create an element like this:
<a href='#' onclick='removeEntry(\'someId\')'><span class='fa fa-times'></span></a>
'<a href="#" onclick="removeEntry(' + element.id + ')">
<span class="fa fa-times"></span>
</a>'
This should work out. You can encode double quotes in single quotes and you want this whole expression as a string.so a string with variable can be written as
string1 = string2 + variable +string3;
Or for a multi lines string in JavaScript you can use back ticks.
`<a href="#" onclick="removeEntry(' + element.id + ')">
<span class="fa fa-times"></span>
</a>`
i have the following code.
<a href="http://url.com" onclick="trackOutboundLink('http://url.com'); return false;">
i simply need to dynamically insert same link as href url and onclick event.
i tried the following but the onclick event didnt work.
<script type="text/javascript">
var url= "https://www.url.com";
document.write('<a href="' + endurl2+ '" target="_top" onclick="trackOutboundLink(' + endurl2+ '); return false;">');
</script>
You have mistyped some quotes. Try:
var url= "https://www.url.com";
document.write('Test');
You can just track whenever a link is clicked and find out the URL from the JS. E.g.
$('body').on('click','a',function(e){
trackOutboundLink($(this).attr('href'));
})
I want to display a div inside Colorbox plugin, but it is not working as there are many double quotes inside the div element and is causing problems so how can I do it.
Here is my code
<script>
$(document).ready(function() {
if(localStorage.getItem('welcomehelloState') != 'shown'){
function openColorBox(){
$.colorbox({transition: "fade", top: "2%", width:"67%", height:"17%", html: "<section class="related">
<p>New Content:-</p>
<a href="/part-2/index>
<img src="img/related/Prdha.png" />
<h3>Art</h3>
</a>
<a href="/part-2/index6">
<img src="img/related/Kavi.png" />
<h3>Games</h3>
</a>
</section>"});
}
setTimeout(openColorBox, 100);
localStorage.setItem('welcomehelloState','shown')
};});
</script>
As you can see there are many double quotes and the brackets are not syncing.
The problem is that when you start a string with a double-quote (") and you place an other quote in the string, JS thinks you ended the string. Thats why the quotes which have to be in the string, have to be escaped: \"
Example: var x = "En then he said: \"Hi there\", with a big smile";
Also, JS does not like linebreaks. you have to escape these too if you need it badly, but the better way is to break the string into multiple strings
Example:
var x = "this is" +
"A multiline" +
"String.";
Your code, escaped well, has to be this:
<script>
$(document).ready(function() {
if(localStorage.getItem('welcomehelloState') != 'shown'){
function openColorBox(){
$.colorbox({transition: "fade", top: "2%", width:"67%", height:"17%", html: "<section class=\"related\">" +
"<p>New Content:-</p>" +
"<a href=\"/part-2/index\">" +
"<img src=\"img/related/Prdha.png\" />" +
"<h3>Art</h3>" +
"</a>" +
"<a href=\"/part-2/index6\">" +
"<img src=\"img/related/Kavi.png\" />" +
"<h3>Games</h3>" +
"</a>" +
"</section>"});
}
setTimeout(openColorBox, 100);
localStorage.setItem('welcomehelloState','shown')
}
});
</script>
If you are using Jquery and making html then make sure there is no enter in lines as jquery won't accept this and it will create an syntax error. use your code in this way..
$.colorbox({transition: "fade", top: "2%", width:"67%", height:"17%", html: '<section class="related"><p>New Content:-</p><img src="img/related/Prdha.png" /><h3>Art</h3><img src="img/related/Kavi.png" /><h3>Games</h3></section>'});
Thank you :)
I am trying to replace quote (') with \' so as to escape escape quote or double quote in string
<ul id="list">
</ul>
<button id="sethrefbtn" onclick="setlink();">Set Link</button>
function setlink(){
var data = {
playerID : 102458,
playername: "Real Madrid's cristiano Ronalado"
}
listring= "<li><a href='SearchServlet?q=" + data.playername.replace(/'/g, "\'"); + "&playerid=" + data.playerID + "&page=1#pg0'>"+ data.playername +"</a></li>";
$("#list").append(listring);
}
Here is fiddle: fiddle
The desired output should be:
Real Madrid's cristiano Ronalado
Your problem is being caused by it being in an HTML attribute (so you need to convert it to an HTML character reference and not escape it). You should deal with that by using DOM instead of string mashing to build your HTML.
However, in this particular case, you are putting it in a URL, so you should be escaping it for URLs first.
You can do that with encodeURIComponent, but since you are building an entire query string and are using jQuery, you can use param instead.
function setlink() {
var data = {
playerid: 102458,
q: "Real Madrid's cristiano Ronalado",
page: 1
}
var url = "SearchServlet?" + $.param(data);
var li = $("<li />").append(
$("<a />").text(data.q).attr('href', url)
);
$("#list").append(li);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id="list">
</ul>
<button id="sethrefbtn" onclick="setlink();">Set Link</button>
listring= "<li><a href='SearchServlet?q=" + data.playername.replace(/'/g, "\'") + "&playerid=" + data.playerID + "&page=1#pg0'>"+ data.playername +"</a></li>";
Just replace the above statement in the code .
There is a semicolon in the middle of the statement in your code.
I have Html contents like
<span>
http://j.mp/eUcRNK
</span>
I want to hyperlink on above html text like this
<span>
<a href="http://j.mp/eUcRNK" class="link" target="_blank">
http://j.mp/eUcRNK
</a>
</span>
How I can do that..
$('span').html(function(i,txt){
return $('<a>').text(txt).attr({'target':'_blank', 'href': txt }).addClass('link');
});
demo
based on the comments below, I guess this solves it.
$('span').html(function(i,txt){
return replaceURLWithHTMLLinks(txt);
});
function replaceURLWithHTMLLinks(text) {
var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&##\/%?=~_|!:,.;]*[-A-Z0-9+&##\/%=~_|])/ig;
return text.replace(exp,"<a class='link' href='$1' target='_blank' >$1</a>");
}
updated fiddle
for jquery 1.3.2, just change the jQuery codes a bit.
var span = $('span');
span.html(replaceURLWithHTMLLinks(span.html()));
another updated fiddle
Try
$("span").each(function(){
var text = $(this).text();
$(this).contents().wrap("<a class='link' href='" + text + "' target='_blank' />")
});