Command to find a specifc text/link on a webpage, using javascript? - javascript

Before I start, I'd like to point out I'm completely new to programming, so I hope my question isn't one that doesn't make sense at all and makes you guys rage. If it is, I'm very sorry, please enlighten me about it.
¨
I'm trying to make a script (in tampermonkey) that will look for a specific text/link on a webpage, and if that text is on the page, it will click on it.
For instance:
Let's imagine we refresh the page and sometimes a link called "Bob" appears. I do not know the target of "Bob", so just asking the script to redirect me to the target link isn't an option. I need something that actually clicks on "Bob". U get me? The problem here is I only know the name of the link, and nothing more. So I'd need a script which, first of all, looks for something called Bob, and secondly, clicks on it.
Thanks for helping,
--Bram
¨
PS: I'm completely new to programming, so if you decide to help me out (which I'd greatly appreciate <3) please explain it like I'm a total idiot/newbie. Thanks!

Just samples are below. Syntax can be wrong but they will give you an idea I think.
//Get all anchors
var anchors = $("a:contains('Bob')")
//Click it
$(anchors[0]).click();
And you can searc on stackoverflow look this links will help you;
How to select all anchor tags with specific text
How can I simulate an anchor click via jquery?

Related

How do i become href out of a link

Following problem:
I would like to not have to copy my navigation bar over every site. So I wrote it in an extra Document and included it with php. The site ur on has a special class to show its active now that where I'm stuck at. I tried following statement just to see if my idea works
<script> if(document.getElementById("test").href == window.location.pathname){window.alert("wow");}
Spoiler alert it does not:(
I know that the pathname is the same as href(tried it) but I can't get the href like that. Does anybody know how to do that? Sorry for my English
Thanks a lot for helping
PS: I'm just getting into js I usually work with other languages but wanted to get into website design.
The problem was simple : document.getElementById("test").href gives out the whole link with www even thoug its not standing in there while window.location.pathname just gives out the path like /index.php. I had to use window.location.href instead.
Thanks for all the answers

Need to take variable input from text box to create a link to use with USPS Tracking System

This is a silly question but I can't seem to find an example that works. Basically I want to add a section on my website for the customer to check their USPS Tracking Number Status; USPS only provides an XML API while I need HTML / JS for this to work.
I need to create a text box that takes a 22 character (or less) alphanumeric variable so that when you click a "Submit" button, it will put the variable into this link.
Sadly, I need the entire script code from the and tags. I have tried every example I found and none of them worked in the sandbox, likely because I am missing some basic tags. I have come to the conclusion that I have no clue what I'm doing so please do not get frustrated by the simplicity of my question.
https://tools.usps.com/go/TrackConfirmAction_input?strOrigTrackNum={xxx} where xxx is must be the variable. Every example of code I have found will append to a link, but not put the variable inside the {}. I have been looking and tried a ton of different examples, but being far from a codemonkey I could really use some help. I apologize in advance because I know this is simple but I don't know what I am doing wrong except for trying to do something I have no business trying to do as I'm not a developer. Thanks in advance!
To provide the answer to my own question here us the correct code,
<script type="text/javascript">
function goToPage() {
var page = document.getElementById('USPS_Tracking_Number').value;
window.location = "https://tools.usps.com/go/TrackConfirmAction_input?>strOrigTrackNum=" + page;
}
</script>

show more/less with FAQ with HTML

I am a newbie with html and i need your support. Please look at my code:
https://fiddle.jshell.net/ghLoau6r/1/
Now I want to make a list of FAQs as table. The requirements are:
when we click to any link button (here we have 2 link buttons as 2 rows of the table), the background must be changed (not pink anymore, but another color)
when we click to any link button, the triangle at the beginning of the row also changes to another form (i created already but my code does not run)
when we click "show info 2", the corresponding "content 2" should be shown, instead of "content 1" now.
please help me, thank you guys very much !
So, not to be rude but there is so many things that need to be fixed here, that I don't have time to share them all with you. I'd strongly recommend picking up a book (HeadFirst HTML & CSS isn't bad, there are plenty others) and getting a solid understanding before continuing the work.
That said, the core problems to your specific question are that first, ID's should be unique (you have two elements with the ID of 'content1'). Second, your second link is looking for content1 still, even though you want content2. A quick fix would be to update the ID of the second content area ,and then update the anchor onclick handler to look for the correct ID.
That said, this is pretty far from an ideal or scalable solution, so please read up.
As commented, I think it will be hard to help without completely writing it for you. But I'd like to at least point out that you should move your JavaScript from inline/in the HTML into a separate JS file.
In your JS Fiddle, take this:
onclick="document.getElementById('content1').style.display=(document.getElementById('content1').style.display=='none')?'block':'none'"
And put it in the "JavaScript" section of JS Fiddle, inside of a $(document).ready() function. Use jQuery to register the onclick event to the behavior you want. Can include jQuery in your fiddle by using a jQuery URI such as https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js
Godpseeed

Creating user options / configuration for a jQuery plugin

I don't know how to write a jQuery plugin BUT I had some funny ideas and developed some kind of "workaround" regardless my lack of skills :D I tried to Google a lot, but I can't realize the correct search string to find what I'm looking for.
Now, my plugin is "working" but I was wondering if somebody can enlighten me to make it look "better", with a properly written code.
You'll probably laugh after I show you what I've done so far , but please, take in mind I'm newbie and I'm just experimenting.
http://jsfiddle.net/Frondor/9za22/
In that jsfiddle you can see I didn't know how to handle "options" for calling dynamic content by the plugin like
var settings = $.extend({
icono: "/images/icon_69.png",
nombre: "Custom name"
}, options );
(Although I don't know what this code does xD But I think that plugins' options should look like these)
but instead of that, I've used an <i></i> (HTML) element as "options holder" with data-attributes in which my script is "looking" for options in order to show another HTML element (let's call it an "alert") which contains dynamic content depending on the data stored in the attributes previously configured inside the <i></i> tag.
If there is not any <i> tag in the page, then no "alert" will be shown.
So
I was wondering if someone can give me a hand to write this "plugin" properly xD With options like this kind of plugin does.
Thanks in advance!

HTML/ AJAX/ JQuery Drill-Down Form

I want to create a html site for troubleshooting problems, in which the user selects an answer to a question and then another question is loaded based on the answer, and then another question based on that answer and so on until they drill down to a solution.
I've looked around on here for something similar but all I've found so far is quite simple code that hides all divs then changes the display options based on which radio button is selected - with the number of questions I want to load I dont think this would be suitable...
I think the best way to do it would be to find a way to store all of the different questions in a separate file then use an onclick event to load the correct one depending on which radio button is selected... preferably using AJAX. Problem is I'm extremely rusty with my js/ajax and have no idea where to begin with this!
I realise it sounds like I'm asking practically asking someone to write it for me haha! but even if someone knows of any tutorials or can just point me in the right direction it would be greatly appreciated!
Try JQuery-Drilldown
A Demo and Instructions are in the Link below. Just look at the tags and the JavaScript

Categories