I need to remove the mailto: from a javascript variable which containts the href attribute of an a element:
var email = $(this).find('a').attr('href');
The output should just be the email address. I try to append the address to a div element:
$(this).append(email);
Simply remove the "mailto:" prefix from the string inside email variable, using substring method:
var emailRef = 'mailto:name#email.com';
// Get the substring starting from the 7th character to the end of the string
var email = emailRef.substring(7);
console.log(email);
var email = $(this).find('a').attr('href');
var address = email.split('mailto:')[1];
//Append to div
$('#divId').append(address);
You can split on the basis of : present in href
var email = $(this).find('a').attr('href');
var onlyEmail = email.split(":")[1]
Just replace it. As simple as
(this).append(email.replace("mailto:","");
change this as:
var email = $(this).find('a').attr('href').split(':')[1]; // gives you email
Here .split(':') will split the string at the index of : and returns an array then you can take the [1] first index to get the email.
For a demo:
var email = $('a').attr('href').split(':')[1];
console.log(email);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Get the mail of this anchor.
After all, it's just text:
"mailto:foo#example.com?subject=Hi"
.replace(/^mailto:([^?]+).*/, '$1');
var email = $(this).find('a').attr('href');
$.each( email, function( key, value ) {
//check for mailto
if (value.startsWith('mailto:')){
//Append to div
$(this).append(address);
}
});
Related
How to put JavaScript value inside HTML ?
I am trying to put JavaScript value inside HTML like below.
<p>
Check your Credit Score Here
</p>
To achieve the results, you can make use of document.getElementById('').href property:
HTML (added the id attribute to the <a> tag):
<p>
Check your Credit Score <a id="link" href="" target="_blank">Here</a>
</p>
JavaScript:
window.onload = function() {
var first_name = 'Peter';
document.getElementById('link').href = 'http://someaddress.com?first_name='+ first_name;
// debug your results
console.log(document.getElementById('link').href);
}
Here is the JSFiddle
do this if you want to change the link
document.querySelector('a').href = "http://someaddress.com?first_name=" + first_name;
You can do like this
<p>
Check your Credit Score
<a href="http://someaddress.com?first_name='+ first_name +'" target="_blank"
>Here</a >
</p>
<script type="text/javascript">
const a = document.querySelector('a');
const first_name = 'John';
a.href = 'http://someaddress.com?first_name=' + first_name ;
</script>
For best practice do an if check otherwise your selector might not be found in the dom.
Also, if in querySelector("...anything...") not querySelector("a") is given the editor won't suggest the href prop that exists or not. Hence, setAttribute makes more sense.
const URL = "http://someaddress.com?first_name="
const name = 'adiat'
const anchor = document.querySelector(".what-ever")
if(anchor){
anchor.setAttribute("href", `${URL}${name}`);
}else{
console.warn("element not found to replace href attribute")
}
// shorthand -> anchor?.setAttribute("href", `${URL}${name}`);
A robust way would be to use a token to replace, I've used {{FirstName}}. Use an attribute selector to select via that token then replace that token on the href attribute
let firstNameLinks = document.querySelectorAll("a[href*='{{FirstName}}'");
let firstName = "Bob";
for(i = 0; i < firstNameLinks.length; i++){
console.log(firstNameLinks[i].href)
firstNameLinks[i].href = firstNameLinks[i].href.replace("{{FirstName}}", firstName);
}
A link
Another link
I tried to get data with JavaScript:
The Text
var link = document.getElementById('link_Page')
var text=link.innerHTML;
var href=link.href;
I expect to see:
"/product/23" and "The Text "
But result is:
"http://localhost:60790/product/23" and "The Text "
Note: on jsfiddle.js I tested and result of text(not link) was fine. couldn't understand why it's gives me ' '
https://jsfiddle.net/mahma/ocwnufqb/
Note: on jsfiddle.js I tested and result of text(not link) was fine
.href will return the full URL of the linked resource, to get the exact value of the href attribute try using Element.getAttribute():
var link = document.getElementById('link_Page')
var text=link.innerHTML;
var href=link.getAttribute('href');
console.log(text);
console.log(href);
The Text
is the space character in HTML. You have a space character in the end of the a tag's text.
Here is the way you can do what you want.
var link = document.getElementById('link_Page')
var text = link.innerText;
var href = link.getAttribute('href');
console.log(text, href);
The Text
I have more than 2000 email addresses. which i have exported from feedburner.
And the email address look like below;
adminvicky#gmail.com Active 12/05/2015 03:07
adminvishal250#gmail.com Pending Verification 8/05/2015 01:07
I want to extract email address from the text file by removing Active, Pending Verification, Date [i.e. 8/05/2015] and time [i.e 03:07] using JavaScript.
I have created a JavaScript Program which something like below which working properly for removing Active, Pending verification text,
<script>
function extracter() {
var a = document.getElementById('input').value;
document.getElementById('output').innerHTML =
a.replace(/Active|Pending|Verification| /g, '');
}
</script>
<textarea id="input"></textarea><br/>
<br/>
<input type="button" value="click" onclick="extracter()"/>
<br/>
<br/>
<textarea id="output"></textarea>
And the output is,
adminvicky#gmail.com 12/05/2015 03:07
adminvishal250#gmail.com 8/05/2015 01:07
And I want the below output. Just help me to remove "Date" and "Time",
adminvicky#gmail.com
adminvishal250#gmail.com
Try this one, i think it will do the job
var a = document.getElementById('input').value;
document.getElementById('output').innerHTML = extractEmails(a).join('\n');
And the function:
function extractEmails (text)
{
return text.match(/([a-zA-Z0-9._-]+#[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi);
}
Here is a fiddle
Here is also an example using jQuery also Extract all email addresses from bulk text using jquery
Try to use this regex:
([a-zA-Z0-9._-]+#[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)
REGEX DEMO
In your Javascript you can implement it like this:
function getMail ( text ){
return text.match(/([a-zA-Z0-9._-]+#[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi);
}
JSFIDDLE DEMO
you can easily write a regex and iterate over the results like:
var reg = new RegExp(/^[\w-\.]+#([\w-]+\.)+[\w-]{2,4}$/g);
var email;
while((email = reg.exec(targetText)) !== null) {
// do something with the email
}
Let's try with this simple regular expression:
var record = ' adminvicky#gmail.com Active 12/05/2015 03:07';
var regExp = /^\s*(.*?)\s+/;
console.log(record.match(regExp)[1]);
You can try this regex instead:
a.replace(/\s+.+$/g, '')
This should work for your case.
I would use string.split(" ") and split the textfile at its spaces.
Example:
var string = " adminvicky#gmail.com Active 12/05/2015 03:07 adminvishal250#gmail.com Pending Verification 8/05/2015 01:07"
var array = string.split(" ");
var emails = [];
for(var i = 0; i < array.length; i++){
if(array[i].indexOf("#") != -1){
emails.push(array[i]);
}
};
Then you have an array emails which contains your email adresses.
Using JQuery load function to read content from .txt file and display email as hyperlink:
$(document).ready(function(){
//Get the text content from txt file using load function
$( "#divid" ).load( "/xyz.txt",function(response, status, xhr){
if(status=='success') {
/* After loading the static text, modifying the email address to hyper link */
var corrected = response;
var emailRegex =/[a-zA-Z0-9._-]+#[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}/g;
corrected.match(emailRegex).forEach(function(email) {
console.log(email);
corrected = corrected.replace ( email, '' + email + '' );
});
$('#divid').html(corrected);
}
});
});
The script in question is posted below. The only line that is giving a problem is:
if(email1_text != email2_text )
And here is the rest of the script
$(document).ready(function(){
$('#email2').focusin(function() {
var email1_text = $('email').val();
var email2_text = $('email2').val();
if(email1_text != email2_text )
$('#email2_span').text('email addresses do not match');
});
});
To get elements by their IDs:
var email1_text = $('#email').val();
To get elements by their class names:
var email1_text = $('.email').val();
These selectors are wrong. So no value will be returned.
var email1_text = $('email').val();
var email2_text = $('email2').val();
Should be something like:
$('#email') or $('.email')
Depending on what email element actually is. A class . or an id #.
Example (not matching)
http://jsfiddle.net/ue8bef92/3/
Example (matching)
http://jsfiddle.net/ue8bef92/5/
Assuming we have a comment textarea where the user can enter this code:
[quote="comment-1"]
How can I replace that code before the form submits with the actual html content from <div id="comment-1"> ?
You could try something like this:
http://jsfiddle.net/5sYFT/1/
var text = $('textarea').val();
text = text.replace(/\[quote="comment-(\d+)"\]/g, function(str,p1) { return $('#comment-' + p1).text(); });
$('textarea').val(text);
It should match agains any numbered quote in the format you gave.
You can use regular expressions:
text = text.replace(/\[quote="([a-z0-9-]+)"]/gi,
function(s, id) { return $('#' + id).text(); }
);
If I understand you correctly, you wish to replace something like '[quote="comment-1"]' with ''.
In JavaScript:
// Where textarea is the reference to the textarea, as returned by document.getElementById
var text = textarea.value;
text = text.replace(/\[quote\="(comment\-1)"\]/g, '<div id="$1">');
In jQuery:
// Where textarea is the reference to the textarea, as returned by $()
var text = textarea.val();
text = text.replace(/\[quote\="(comment\-1)"\]/, '<div id="$1">');
Hope this helps!