how do I swap text after comma delimiter? - javascript

The following code is currently being generated and produces 'LastName, FirstName'
<div id="welcomeMenuBox">
<spanid="zz4_Menu_t" class="ms-menu-althov ms-welcome-root">
<a id="zz4_Menu" class="ms-core-menu-root" title="Open Menu" href="javascript:;">LastName, FirstName</a>
</span>
</div>
I would like to swap the text on page load so that it says Welcome FirstName, LastName in either JQuery or JavaScript.

You can use:
var text = $('#zz4_Menu').text().split(',');
$('#zz4_Menu').text('Welcome ' + text[1] + ', ' + text[0]);
Fiddle Demo

Here is one method -
var currentText = $('.ms-core-menu-root').text();
var arrayText = currentText.split(',');
var newText = 'Welcome ' + arrayText[1] + ' ' + arrayText[0];
$('.ms-core-menu-root').text(newText);

JavaScript :
var a = document.getElementById('zz4_Menu')
var res = a.innerHTML.split(",")
a.innerHTML = "Welcome " + res[1] + "," + res[0]
Example

Related

How to keep the next line \n from getting removed inside the email text?

All the \n are removed from the string when the text is put inside mail body .
var valuess = Object.entries(feedBackText);
valuess.forEach(function (key) {
responseText = responseText.concat(' ' + key[0] + ':' + key[1] + '\n');
});
var parsedString = responseText.toString();
window.location = "mailto:myid#gmail.com"+"?subject="+subjectmail+"&body=" +
parsedString;
The following demonstrates how you can solve this using the built-in encodeURIComponent function:
var parsedString = "text on the" + "\n" + "next line";
var link = "mailto:myid#gmail.com" + "?subject=Example&body=" +
encodeURIComponent(parsedString);
console.log(link);

HTML concatenation issues in JS

I am trying to make a dynamic HTML which I'll append using jquery before append if I'll console the HTML everything looks fine but after append in browser The whole structure messed up.
Here is my HTML :
<script>
var title = "my title";
var toolbar ='<hm_toolbar user="current_user " upp="hm" index="hmIndex "></hm><synapse_toolbar uuid=" hm.hm_pk " my-method="get_linked_facts" ng-if=" flag == true "></synapse_toolbar>';
var map_html = '<a onclick="panToMarker(' + lat + ',' + long + ')"> ' + title +'"</a>' + toolbar ;
var li_html = "$('#" + title + "').append('<li class=\"list-group-item\"><div dynamic=\" " + map_html + " \"></div></li> ')" ;
var g =title.replace(/ /g,"_");;
var fn = "function "+ g +"(){ console.log('--working--'); "+ li_html +"; }";
console.log(fn)
eval(fn);
}
</script>
When the above li_HTML does its work means append the HTML the appended is all messed up
Appended HTML :
<li class="list-group-item"><div pantomarker(49.711083,6.251445)"="" dynamic=" <a onclick="> Test"<hm_toolbar index="hmIndex " upp="hm" user="current_user "></hm_toolbar><synapse_toolbar ng-if=" flag == true " my-method="get_linked_facts" uuid=" hm.hm_pk "></synapse_toolbar> "></div></li>
I know I have messed up the concatenation using with quotes but I am not able to fixed the bug .
You said that the console.log() looks OK but you have eval() after that which means that you have to escape some quotes twice.
Try this:
var toolbar ='<hm_toolbar user=\\\'current_user \\\' upp=\\\'hm\\\' index=\\\'hmIndex \\\'></hm><synapse_toolbar uuid=\\\' hm.hm_pk \\\' my-method=\\\'get_linked_facts\\\' ng-if=\\\' flag == true \\\'></synapse_toolbar>';
var map_html = '<a onclick=\\\'panToMarker(' + lat + ',' + long + ')\\\'> ' + title +'</a>' + toolbar ;

counting string character in javascript return always 1

Hi everybody this code is used to have a list name and id of facebook friends or invited friends if executed on friend list page. I'm trying to count the character of a string in javascript but .length method return always 1. I don't understand why cause I'm counting on a string not an array.
this is my code:
var name_list;
var id_list;
var count_letter_l;
var count_name = 0;
var count_id = 0;
var inputs = document.getElementsByClassName('_2akq _1box');
for(var i=0;i<inputs.length;i++){
var name = inputs[i].getElementsByTagName('span')[0].childNodes[0].nodeValue;
var full_id = inputs[i].getAttribute("data-reactid");
var split_id = full_id.split(':');
var split_two = split_id[1].split('.');
var split_final = split_two[0];
var count_letter = split_final.length;
//console.log(count_letter);
if(name != 'null'){
name_list+= ',' + '"' + name + '"';
id_list += ',' + '"' + split_final + '"';
count_letter_l += ',' + '"' + count_letter + '"';
count_name++;
count_id++;
}
}
console.log(name_list);
console.log('------!!!!!!!!------');
console.log(id_list);
console.log('names = ' + count_name);
console.log('id = ' + count_id);
console.log('letters for esch field = ' + count_letter_l);
I wish to count the character of every id cause in my case when I grab the ids I have some "0" and "1" in the and of the list. I don't know why and I wish to cut them out of the list.
This is an element of _2akq _1box class. you can see it if open firefox firbug and look at facebook front-end html code while you are displaying the friends list
<span class="_2akq _1box" data-reactid=".5q.2.0.0.0.0:0:1:$1543522353.0.0.$2.$text.0.0">
<span data-reactid=".5q.2.0.0.0.0:0:1:$1543522353.0.0.$2.$text.0.0.0">Laura Casali</span>
</span>
the console tell me:
letters for esch field = undefined,"1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1".....
tnx for help

Two Contents Added With Space

I'm trying to add contents from two DIVs and inject them into an input field. The following works except there is no space in between the values entered.
With the code below the values in the text field is: JohnDoeAccountant
I'm looking for an output in the text field of: John Doe Accountant.
How could I ensure there is a space in between the values of the outputs?
var output1 = document.getElementById("firstname").innerHTML;
var output2 = document.getElementById("lastname").innerHTML;
var output3 = document.getElementById("job").innerHTML;
document.getElementById("user-submitted-tags").value = output1 + output2 + output3;
Try
document.getElementById("user-submitted-tags").value = output1 + ' ' + output2 + ' ' + output3;
....value = [output1, output2, output3].join(" ");
Or cut out some repetition:
document.getElementById("user-submitted-tags").value =
["firstname","lastname","job"].map(function(id) {
return document.getElementById(id).innerHTML;
}).join(" ")
Or like this:
document.getElementById("user-submitted-tags").value =
["firstname","lastname","job"].reduce(function(s, id) {
return s + " " + document.getElementById(id).innerHTML;
}, "")

Javascript variables email link - "To" field is blank

How can I do this:
var newContactEmail = "abc#cc.com";
document.getElementById('contactEmail').innerHTML = '' + newContactEmail + '';
When I click on the mail link, it opens but the "To" field is blank.
You are putting the closing double quotation mark too soon.
var newContactEmail = "abc#cc.com";
document.getElementById('contactEmail').innerHTML = '' + newContactEmail + '';
Instead of href="mailto:"' + newContactEmail + '" ' try href="mailto:' + newContactEmail + '" '.. Else it would look like href = "mailto:"someemail#a.com .

Categories