JQuery: How to do comparison of variables? - javascript

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/

Related

i have problem with multiple form with the same id with google tag manager

Hi i have many forms inside multiple pages all of them the with the same id (success message) after submitted and same class names when i'm sending the form which e.g inside home page i put element selector through id with Page PATH with match regex something like that \/(en|es)\/ it works good without problem ... but when i'm going to page www.something.com/send-something/233?search=profile the form submitted through old id which was for home page i tried to inject custom javascript something like :
function() {
var els = document.querySelectorAll('#sendReqSurgyForm1');
for (var i = 0; i < els.length; i += 1) {
if (els[i] === {{Page URL}}) {
return i;
}
}
return '(nothing sent)';
}
with adding matching Page URL with match regex something like that to https:\/\/www\.something\.com\/(ar|en)\/send-something\/[0-9]+\?source=[a-zA-Z]+\_?[a-zA-Z]+
to matching the url: www.something.com/send-something/233?search=profile
the trigger always works with home page but trigger which located in www.something.com/send-something/233?search=profile not success and the result of javascript always returns nothing sent .. please help to fix this problem
Hi the Answer is SPA Angular using SPA so it can be multiple form with the same component so i solved this problem through setting id for every form with different urls
for example if you have multiple forms in angular inside multiple routing page as i explained in the main topic question .. to fix this problem you can setting id for every form submissions through js by setting new id for every form submission by setting Attribute for id based different urls.
main_id = exampleForm
function(){
var ids = document.getElementById("exampleForm");//main id
var current_url = window.location.href;//current url of visitor
var dir_match = location.pathname+location.search;//the path of website after .com
var send_req = 'https://www.example.com/'+dir_match.match(/[ar][r]|[en]n/)+'/surgery-request/'+dir_match.match(/[0-9]+/)+'?source=profile';//link1 which have form1
var send_req2 = 'https://www.example.com/'+dir_match.match(/[ar][r]|[en]n/)+'/surgery-request/'+dir_match.match(/[0-9]+/)+'?source=profile2';//link2 which have form2
var home_url = 'https://www.example.com/'+dir_match.match(/[ar][r]|[en][n]/)+'/';//link3 which have form3
if(current_url == send_req){
/* if current_url equal to link1 set new id e.g = `forms_req_surgery` */
last_send_req = ids.setAttribute("id", "forms_req_surgery");
var elm = document.getElementById('forms_req_surgery');
var last_var = elm.id;/* get the name of id */
return last_var; // return the name of id after changed set
}else if(current_url == home_url){
last_home_url = ids.setAttribute("id", "home_req");
var elm2 = document.getElementById('home_req');
var last2_var = elm2.id;
return last2_var;
}else if(current_url == send_req2){
last_send_req2 = ids.setAttribute("id", "forms_req_surgery2");
var elm3 = document.getElementById('forms_req_surgery2');
var last3_var = elm3.id;
return last3_var;
}
}

How can I add data params to an HTML string and get a string back?

I got a string like this:
var select_string = '<select><option>1</option><option>2</option></select>';
I need to add some data params to select in this string and get this string back in order to get the following:
select_string = '<select data-param1="param1" data-param2="param2"><option>1</option><option>2</option></select>';
I tried to use jQuery functions like .html() or .text() but it did not work. Like this:
select_string = $(select_string).data('param1', 'param1').html() //or .text()
Any ideas how to make it work would be helpful. Thank you.
You can use attr to add that attributes to the element
var select_string = '<select><option>1</option><option>2</option></select>';
var select = $(select_string).attr({
'data-param1': 'param1',
'data-param2': 'param2'
});
console.log(select.prop('outerHTML'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Since your attribute name starts with data-, if you want to get the value, you can use:
select.data('param1'); // param1
select.data('param2'); // param2
EDIT: Titulum is right, jquery is not needed here.
But here is the working example usign jquery
var selectString = '<select><option>1</option><option>2</option></select>';
var $select = $(selectString);
$select.attr("prop_key","prop_value");
var selectChanged = $select.prop('outerHTML');
console.log(selectChanged)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
You don't need jQuery for this:
const myElement = document.createElement('div');
myElement.innerHTML = "<select><option>1</option><option>2</option></select>";
const selectElement = myElement.getElementsByTagName("select")[0];
selectElement.setAttribute("data-param1", "param1");
selectElement.setAttribute("data-param2", "param2");
You could use indexOf and substr() to split it into 2 parts, insert your new text, and put it back together again.
var first_half = select_string.substr(0, select_string.indexOf('>'));
var second_half = select_string.substr(select_string.indexOf('>'));
select_string = first_half + ' data-param1=\"param1\" data-param2=\"param2\" ' + second_half;

jQuery do not accept new fields in HTML

I try to create a generator for lucky numbers. I did it with C# and now with JavaScript and jQuery. You can see this here. When I add new field - JQ do not see it. Just try to click on numbers in "Your field". I have as standard 7 fields and they work fine but when I add new line script do not recognise it like something useful. Could you give me some advice?
change below js code. check this working plunker
Your code:
$('.spielzahlen').on('click', function() {
var list = []
tablereset()
var item = $(this).text().split(', ')
item.forEach(function(index) {
if (index != "")
list.push(index)
})
console.log($(this).text())
list_temp = list
$(this).empty()
$('#temp').val(list)
var tds = document.getElementsByTagName("td")
list.forEach(function(index) {
var idt = index - 1
tds[idt].className = 'checked'
})
changeLen(list_temp.length)
})
Change it with below code , there is only change in initialization other code are same :
$(document).on('click','.spielzahlen', function() {
var list = []
tablereset()
var item = $(this).text().split(', ')
item.forEach(function(index) {
if (index != "")
list.push(index)
})
console.log($(this).text())
list_temp = list
$(this).empty()
$('#temp').val(list)
var tds = document.getElementsByTagName("td")
list.forEach(function(index) {
var idt = index - 1
tds[idt].className = 'checked'
})
changeLen(list_temp.length)
})

Extract email addresses from a text file using JavaScript

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

Using document.getElementById() inside a function

I have this code:
<div id="com_22">
<a onclick="delete(22);">delete entry</a>
</div>
and the javascript code:
function delete(url){
var tupu = document.getElementById("#com_"+url+"");
alert(tupu);
}
the problem is I'm having a null alert
I want to get the id
document.getElementById("#com_22");
How I can solve it
update
this is my code:
function confirmDel(url){
$(document).ready(function(){
$.ajax({
type: 'POST',
url: '/files/assets/php/ajax/blog-stream.php?act=del',
data: 'url=' + url ,
success: function(h){
var status = h.status
if (status == "si" ) {
$("#com_"+url+"").fadeOut("slow");
}
}
});
});
}
the code excecutes so well except for the id didnt fade out
$("#com_"+url+"").fadeOut("slow"); <--- no working
See :: getElementById(), that should have been:
function delete_something(url){
var tupu = document.getElementById("com_"+url);
alert(tupu);
}
btw, try avoiding use of js pre-defined names as function name see:: delete
Don't include the hash when you're selecting by id. You're not using jQuery or querySelector.
var tupu = document.getElementById("com_"+url);
There is also no need to concatenate the empty string there at the end so I removed it.
Remove the '#' from within the bracket.
The function getElementById gets the element ID as a string, not a CSS-style selector. Change to:
var tupu = document.getElementById("com_"+url);
As can be extracted by javascript levels:
[{"file": ". m3u8" in this page: http: //ivm.antenaplay.ro/js/embed_aplay_fullshow.js? id = 6eI6sLVBfOL

Categories