Save parameter from link on page as var in javascript [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I apologize for the lack of code on this one. I am totally lost on a starting point.
On a website there is a link such as the following:
This Is My Link
I am trying to get the accountId of 00T122233ABCEFG123 and use it as a variable. The link that appears above does not have an id, so there is no way to target it other than to find "accountId" on the page, as it only appears in the page once.
Any help is greatly appreciated.

You could use a selector to target the element based on the href attribute, and then extract the value through the URL api
const link = document.querySelector('a[href*="accountId"]');
const href = new URL(link.href);
const id = href.searchParams.get('accountId');
console.log(id);
This Is My Link

You can select it using the attribute contains selector :
a[href*=accountId]
Otherwise, maybe its parent has an ID? There are many ways to target an element in a page.

Related

Call function inside href using Jquery [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
i have this link
Test
Now when i click directly on the link .. The alert get popped up in my window.
But, $("#myLink").click();
doesn't call the popup.
EDIT : I need to trigger the function inside href using jQuery
Solved --------- :
var x = $('#myLink').attr("href");
window.location = x;
Figured out..
var x = $('#myLink').attr("href");
window.location = x;
getting the href value and then targeting it .. solved the problem.
Try href="javascript:alert ('test')" or onclick="alert ('test')"

get webpage html element values without opening the page in new window or new tab using JavaScript [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Just like the title says I need a way to get the HTML of a webpage with out opening it in a new tab or window. I am making a chrome extension that will take element values from one page and append them to another page.
var htmlObject = gethtml("url");
would like some thing like this
You can use jQuery to get the content of the webpage:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
var htmlObject;
var yourURL = 'url.html';
$.get(yourURL, function(html) {
htmlObject = html;
});

How to get all hyperlink urls and create onclick in javascript [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have to get 3 hyperlinks in javascript based only on their href and create an onClick event. In other words, I need to do two things with the below urls:
Get all three a elements based on their URLs, for instance href1,href2,href3
Create an onClick event, for example href1.onClick calls alert("I am from India");
URLs:
<a class="" href="/MyProject/Information/EmpDetails.aspx?userId=79874&countryId=875567" title="">India</a>
<a class="" href="/MyProject/Information/EmpDetails.aspx?userId=98667&countryId=98755" title="">Australia</a>
<a class="" href="/MyProject/Information/EmpDetails.aspx?userId=76456&countryId=87463" title="">New Zealand</a>
Thanks everyone in advance
You can use jquery attribute selector to find the elements, like:
$("a[href$='countryId=875567']").click(function(){
alert("countryId=875567");
});
This will get all hyperlinks that ends with countryId=875567.
If you want the exact href you should use:
$("a[href='http://example.com.br/test']").click(function(){
alert("test");
});
Example: http://jsfiddle.net/v3qpy3gL
Hope it helps.

Loop over HTML class to add a different ID to each HTML element with a specific class [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am trying to loop through an HTML page to check HTML elements with class="details" and then each element will that class with get an ID added to it. Of course the ID will be different for every HTML element that has class="details". Below is what I started to code, but I realized that if I have 20 HTML elements with class="details" that there has to be a way to loop or write less code to do what I want. Any help will be greatly appreciated.
$('.details').eq(0).attr('id' , 'cLc');
$('.details').eq(1).attr('id' , 'bLt');
Something like this?
$('.details').each(function(idx) {
$(this).attr('id', 'id-' + idx);
});

Nested collection-Form, append to nearest class [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Hello i have been trying to make a dynamic collection that i can post to the server, after some struggeling i found this guide;
http://jarrettmeyer.com/post/2995732471/nested-collection-models-in-asp-net-mvc-3
Great written but i havent got it to work for my needs.
Everything works fine exept one "little" annoying thing.
First some information about what im trying to achive;
My classes looks like this;
Qpack has a list of questions
question has a list alternatives
The interface that i have created looks like this;
And this is the markup.
The "add Question"-button works great and the markup match, The thing that dosent work is that wen i click on "Add Alternative" it is always being added to the first question. But the markup is fine as seen in the second picture.
The function responsible for the append looks like this;
function addNestedForm(container, counter, ticks, content) {
var nextIndex = $(container + " " + counter).length;
//var nextIndex = $(counter).length; // Orginal
var pattern = new RegExp(ticks, "gi");
content = content.replace(pattern, nextIndex);
$(container).append(content);
resetValidation();
}
I want to append to the most relative "alternatives" but it seems that it always goes for the first, any idea how to get it to understand the "nearest" alternatives?
When a jQuery selector specifies an ID (#) and there are multiple IDs in the Html document that have that Id jQuery will always return the first.
You must have a way to specify the "Alternatives" uniquely throughout your page.
Alternatively (pun intended) you can create a new css class, replace
<div id="alternative" ...
with
<div class="alternative-container" ...
Then on your action of "Add Alternative" you can
var container = $(this).parents('div.alternative-container:first');

Categories

Resources