A href to link to javascript - javascript

Probably something simple so sorry!
Currently, I have <button id="pin">Get pin</button> and it works. (there is a script later on that this calls).
But, I want it to work on some text - preferable an <a href> as the styles are set up on this - so something like Get pin - obviously this would not work so would like to know how to get it to work! :)
I am trying to get it to run the below when clicking it:
<script>
$("#pin").click(function() {
swal("Your support pin", "Your support pin is: {$support_pin}", "info")
});
</script>
Thanks

Change href to "#" and add a click listener and it should work
Get pin

Related

Detecting which link is clicked with pure Javascript

Here's the code:
for eachP in arr14:
print('<a class="link" href="javascript:void(0);" id="{0}">{1} onclick=""</a><br />'.format(arr14.index(eachP), eachP))
Now, this code is in working state but, I want to alter the code and do something with the onclick="" attribute.
I would like to detect which link user has pressed and alerting the exact same link.
The code that I was writing was this:
onclick="<script>alert(document.getElementById(" + arr14.index(eachP) + ");)</script>"
Note: It's a .py file and I am using HTML, JS and Python(of course) inside it.
I am having problem with the onclick event. I would highly appreciate if anyone could help solve my problem ^.^
If I'm not mistaken you can do that:
onclick="someMethod(this)"
function someMethod(input) {
alert(input.id);
}
jsfiddle

Chrome Extension - Grabbing link from Reddit post after upvoting

I"m trying to make a chrome extension. One of the functions of the extension is to grab the link of the post that the user has upvoted. I'm a little confused on how to go about it...
This what I have gotten to work on it so far as far as testing is:
document.addEventListener('click', function(e) {
if (e.target.matches('.arrow.upmod')) {
alert("Hi");
}
});
Which just displays "Hi" when you click the upvote button. I was just testing to see if it actually detects the vote change.
I was playing around with some jQuery, and if you write:
$('div .arrow').parent().parent()
you get the with the information I need. Here is what it returns:
<div class=" thing id-t3_5env7c odd link RES-keyNav-activeThing" id="thing_t3_5env7c" onclick="click_thing(this)" data-fullname="t3_5env7c" data-type="link" data-author="randy001rd" data-author-fullname="t2_rq7wc" data-subreddit="videos" data-subreddit-fullname="t5_2qh1e" data-timestamp="1479996207000" data-url="https://www.youtube.com/watch?v=f72WZwvMTj4" data-domain="youtube.com" data-rank="1" data-context="listing">
What I need is the data-url and data-domain from the post that is upvoted. Any help or direction would be greatly appreciated. Thanks!
jQuery provides a .data method that you can use to retrieve the fields. $("div.arrow").parent().parent().data("url") should give you the data-url field, and getting the domain is similar.
alert($("div").data("url"));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="thing id-t3_5env7c odd link RES-keyNav-activeThing" id="thing_t3_5env7c" onclick="click_thing(this)" data-fullname="t3_5env7c" data-type="link" data-author="randy001rd" data-author-fullname="t2_rq7wc" data-subreddit="videos" data-subreddit-fullname="t5_2qh1e" data-timestamp="1479996207000" data-url="https://www.youtube.com/watch?v=f72WZwvMTj4" data-domain="youtube.com" data-rank="1" data-context="listing">

Track JavaScript event

I have GA on my website and i'm trying to track every click on the website. The following JavaScript must be used, it acts like an overlay on the page:
<script type="text/javascript">
var tile = new HTMLLiveTile.TileController();
window.onmousedown = function() {
tile.openStoreProduct("var1", "var2", "var3");
}
</script>
What would be the HTML equivalent code to track this?
Right now i have:
<a id="tile" onClick="ga('send', 'event', 'click1', 'click2', 'sample');"><img src="./images/image.png"> </a>
I'm very new to this, sorry if it's redundant. My assumption was to track the variable and add it to the onClick.
I guess you are asking for a solution that does not require much hassle and much changing in code and moreover without any need to change your previous code.
you can use event.target.+ things you need to know to get info.
function mouseTrack(){
var element_name = event.target.tagName;
alert("mouse click was detecteted at: "+ element_name);
}
window.addEventListener('click', mouseTrack(), false);
this code will alert the Tag Name of Element clicked (like DIV, a, SPAN etc you know the list.). But this code is awful.It wont work in Mozilla FF, It wont work In IE 9 below. I took some time to create a fiddle on JSFiddle.net you can view example I made Here

jquery "if" something and "else" something

I am just curious how would anyone make this. I am trying to figure out with lack of knowledge and of course I cannot make it.
So it would be something like...
If in jquery there is declared onClick function something like "firstGateway" and "secondGateway" how can I add what if there is first and what if its second.
I can't even explain well.
But let me try.
<a onClick="firstGateway">YES FIRST!</a>
That would be html piece and jquery would need to run following:
<script type="text/javascript" src="http://fileice.net/gateway/mygate.php?id=492b542f45684b42"></script>
onClick=startGateway('123456');
and if it would be in html like this:
<a onClick="secondGateway">YES SECOND!</a>
Then jquery would run following:
<script type="text/javascript" src="http://fileice.net/gateway/mygate.php?id=4465766c4278366467523838"></script>
onClick=startGateway('654321');
Hope you understand me. I will still try to make it work but I do not think I will be able to succeed.
$('a').click(function(e){
if (e.target.innerHTML == "something")
//fooo
else
// Bar
});
You can check anything you want inside the callback. e.target is the anchor that was clicked.
if (e.target.id == "someId")
if ($(e.target).hasClass('fooClass'))
With your current code, if someone clicks on the link nothing happens. Let's fix that first of all:
This:
<a onClick="firstGateway">YES FIRST!</a>
Should be this:
<a onClick="firstGateway()">YES FIRST!</a>
If you want to execute the function firstGateway() whenever the user clicks that link. However, this is still not the best way and I'm going to show you a better way below. (Note that this better way is also needed for my final solution).
Now we turn this into:
<a id='gateway1'>YES FIRST!</a>
No longer do we define the event in the HTML, instead we do so in our javascript using jQuery:
$(document).ready(function ()
{
$('a#gateway1').click = firstGateway; // Do note: this time around there are
// no brackets
}
Using this, you can now do several things. First, you could do this:
$('a#gateway1').click();
It simulates a click on the link, which I believe does the thing you wanted to do.
However, in order to write your code, you have made sure that you knew what function you were connecting to it in your javascript, so you might not even need such a solution anymore as you should be able to do this:
$(document).ready(function ()
{
$('a#gateway1').click = firstGateway; // Do note: this time around there are
// no brackets
firstGateway();
}

jQuery - Click on X, grab the NAME, then use that to show a related element

Here's what's happening.
My page loads. Then jQuery loads a feed which is injected into a contentCol div...
in that feed is:
Comment
There is also:
<div class="storyItemComments" id="storyItemComments_66" style="display:none;">....
For some reason this is not working:
$(".commentWrite").live("click",function(){
cmt2open = $(this).attr('name');
$("#" + cmt2open).show();
return false;
});
Any ideas? The only thing I can think of is that it has something to do with the fact that I am using jQuery AJAX to load in the content which the LIVE statement above is referencing....
thanks
on most occasions people should use the die() function before the live... so that will clear any previews instructions and that function is only triggered once...
another thing could be that the instructions are called before the contents are retrieved from the ajax. therefore you should use .success to check if the ajax was successfully loaded and then trigger the click instructions.
as it seems that the <div id="storyItemComments_66" has not been picked up from the DOM.
Following from your comment to this answer
Can you try the following instead of show()?
$("#" + cmt2open).attr('display', 'block');
Can you place your javascript inside;
$(document).ready(function(){
//your code here
});
Not sure this will fix it but it's good practice I find.
Also have you alerted out your variables to see what you get?
What is the error you are getting if any?
Have you tried putting an alert at the top of the function to see if the click event works?
edit
let me know if the above does not fix it and I'll remove this answer to clear out the noise

Categories

Resources