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

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.

Related

Save parameter from link on page as var in 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 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.

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);
});

how to show div using it's ID and change the visibility from none to block? [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 need a javascript code that gets a hidden div by Id and changes the style from display:none; to display:block; .
HTML:
<div id="post-author" style="display:none;">by samuel</div>
every post in my site has that html code on beginning. how can i be able to print or show element by it's Id ? thanks !
<script>document.getElementById("post-author").style.display="block";</script>
This will do what you're after :) Just put that in the head of your site
This has been answered a million times before... but here you go:
document.getElementById('post-author').style.display = 'none';
and
document.getElementById('post-author').style.display = 'block';

How do I make every TR tag in a table a link with Perl and 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 8 years ago.
Improve this question
If I have a tag called "post" and has href="something from perl" what should i use for javascript in order to make it clickable and redirects me to the link?
If you implement jQuery you can do something like this:
jQuery(document).ready(function($) {
$(".click").click(function() {
window.document.location = $(this).attr("href");
});
});
edit
assuming your tag looks like this
<tr class = "click" href = "<%= something from Perl %>">

Contents between tags to turn into title JS [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 have <a class="wishlist">TEXT</a>
i need <a class="wishlist" title="TEXT"></a>
I can use only JS.
As your selector is a class, I'd set the function up to handle the possibility of multiples.
$('.wishlist').each(function() {
$(this).attr( 'title', $(this).text() ).empty();
});
Don't forget to either place this code after your HTML, or wrap it in document.ready.
$('.wishlist').attr('title', $('.wishlist').text() );
$(".wishlist").attr("title", $(".wishlist").text()).text("");
That will set the title attribute to the current text, and afterwards unset the text.
try it again:
$('.wishlist').attr('attr','TEXT').html("");
document.getElementsByClassName('wishlist').title = 'TEXT'
document.getElementsByClassName('wishlist').innerHTML = ''

Categories

Resources