How to take html element without text? - javascript

Edit:
sorry for bad explanation so i have:
var x = $("span", this).get( 0 );
var y = x.outerHTML
and it give me:
<span>'some text'</span> but i want just only tag <span></span> with all atributes. My question is how to get it?;p
thanks for any help!

The problem in your code:
var x = $("span", this).get( 0 ); <-- you have parent (even TAG name) and taking first child
var y = x.outerHTML <-- here you're trying to get parent (you eant - TAG name) :%
So, if you know var x = $("span"... <---- this string - you can easily create a string you want
var tag = "span";
var x = $(tag, this).get( 0 );
var y = x.outerHTML;
// the most dumbest way
var result = '<'+tag+'>'+'<'+tag+'/>';

cloneNode() by default does not copy the element content, so you can use it:
var x = $("span", this).get( 0 );
var y = x.cloneNode();
var z = x.outerHTML;
In your function of fiddle it would be something like that:
var x = tur.cloneNode();
console.log(x.outerHTML);

Related

How read the next value in the same line?

I want to read the value "-2.5" in variable "x1", and the value "0.4" in other variable called "y1". And the same for the line below: read "12.1" in a variable "x2" and read "7.3" in a variable "y2"
var lines = [
"-2.5 0.4",
"12.1 7.3"
];
var x1 = parseFloat(lines[0]);
var y1 = jQuery(x1).next();
var x2 = parseFloat(lines[1]);
var y2 = jQuery(x2).next();
console.log(x1);
console.log(y1);
console.log(x2);
console.log(y2);
this is the problem i'm solving
and the code i've made so far, but not accepting "Wrong Answer 85%"
Uncaught ReferenceError: jQuery is not defined
Your error can be solved by investigating whether Jquery is loaded or not, but it won't give you the correct result with your current code.
You can do this:
var lines = [
"-2.5 0.4",
"12.1 7.3"
];
var lineParts = lines[0].split(" ");
var x1 = parseFloat(lineParts[0]);
var y1 = parseFloat(lineParts[1]);
lineParts = lines[1].split(" ");
var x2 = parseFloat(lineParts[0]);
var y2 = parseFloat(lineParts[1]);
console.log(x1, y1, x2, y2);
I think you can have a look over this solution for your reference, hope this help
var lines = [
"-2.5 0.4",
"12.1 7.3"
];
// This will convert a string into an array seperated by space (" ")
const generatePosition = string => string.split(" ");
// This will map all our initial values and change them into a full array
const pos = lines.map(string => generatePosition(string));
console.log(pos);
// From now you can freely access the variable the way you want, this is just for sample demo
const a = pos[0][0];
const b = pos[0][1];
console.log(a);
console.log(b);
Can you reformat your lines array so that each value is in its own element? That would make it easier to access each element. Like this:
var lines = [
Array("-2.5","0.4"),
Array("12.1","7.3")
];
Then you could access the values this way:
var x1 = parseFloat(lines[0][0]);
var y1 = parseFloat(lines[0][1]);
var x2 = parseFloat(lines[1][0]);
var y2 = parseFloat(lines[1][1]);

concat two variable and find array value position using javascript

I have array object like this below
var TTP01[2,0,0,0,0,0,4,6,1,4,0,9,1]
If I assign TTP01[0] like this, I will get Output 2. This is working fine.
But I'm getting values separately and I need to assign the Object.
object = TTP;
count =01;
xy = x*y;
I concat like this below
var obj = objname.concat(count, "[", xy, "]");
console.log( obj );
In console log, I'm getting like this TTP01[0].
But want to get output 2
Please help me... Thanks
This will work.
eval(objname + count)[xy]
fullcode:
var TTP01 = [2,0,0,0,0,0,4,6,1,4,0,9,1];
var objname = "TTP";
var count = "01";
var xy = 0;
console.log(eval(objname + count)[xy]); // 2
You can try like this way,
var TTP01 = [2,0,0,0,0,0,4,6,1,4,0,9,1];
var objname = 'TTP';
var count = '01';
xy = 0;
var obj = window[objname + count];
console.log( obj[xy] );
Assign TTP01 to some base object :
var base = {
TTP01: [2,0,0,0,0,0,4,6,1,4,0,9,1]
}
var objname = 'TTP';
var count = '01';
var objStr = objname + count;
var xy = 0;
console.log(base[objStr][xy])

Adding values concatenating

Instead of "var instance = ..." adding the two values it concatenates them. Can anyone suggest what I need to fix?
I'm trying to add "var startingEmail" value and "var k".
Thank you for your help!
var startingEmail = sheet.getRange("C2").getDisplayValue();
var numEmails = sheet.getRange("E2").getDisplayValue();
var max = numEmails;
for (var k = 0; k<max; ++k){
var threads = GmailApp.getInboxThreads(startingEmail,max)[k]; //get max 50 threads starting at most recent thread
var messages = threads.getMessages()[0];
var sndr;
var rcpnt;
var srAry = [];
var sndr = messages.getFrom().replace(/^.+<([^>]+)>$/, "$1"); //http://stackoverflow.com/questions/26242591/is-there-a-way-to-get-the-specific-email-address-from-a-gmail-message-object-in
var sndrLower = sndr.toLowerCase;
var rcpnt = messages.getTo().replace(/^.+<([^>]+)>$/, "$1");
var rcpntLower = rcpnt.toLowerCase;
var cc = messages.getCc().replace(/^.+<([^>]+)>$/, "$1");
var ccLower = cc.toLowerCase;
//srAry.push(sndr);
//srAry.push(rcpnt);
//srAry.push(cc);
var isIn = joinAddr.search(sndr || rcpnt);
if(isIn == -1){
var instance = k;
I can't see the example in your code but it sounds like you can just wrap Number() around your variable and it will perform the type conversion so the code will perform the math instead of concatenating as strings.

Replace dynamically generated text elements

I am working on some code that is using dynamically generated graphs. And all of these graphs have legends at the y-axis. Now my goal is to check how long the longest legend-string is, and if the longest one is bigger than 20 characters, I only want to show the first characters of every string.
With the code below, I achieved that i can alert the desired shortened strings; but I do not know how to change the text now with these new strings.
var textLengthArray = [];
var labelStrings = domContainer.find(" g > .brm-y-direction > .tick > text");
labelStrings.each(function() {
textLengthArray.push($(this).text());
});
var longestString = textLengthArray.sort(function(a, b) {
return b.length - a.length;
})[0];
if (longestString.length >= 20) {
$("g.tick text").css("font-size", "9pt");
var offsetLeft = longestString.length * 3.7;
textLengthArray.map(function(sub) {
var subString = sub.substring(0, 6);
alert(subString);
});
};
I have tried something like:
$(labelStrings).replaceWith(subString)
With this, I had no legend at all, since I have replaced the whole text tag and all of its attributes with the new string.
So is there any way of not touching the tag and its values at all, but simply change the text between the opening and closing tag?
Thanks in advance!
sth. like this?
var $nodes = domContainer.find(" g > .brm-y-direction > .tick > text");
var longestLength = 0;
$nodes.each(function(){
longestLength = Math.max(longestLength, $(this).text().length);
});
if(longestLength >= 20){
$("g.tick text").css("font-size", "9pt");
var offsetLeft = longestLength * 3.7;
$nodes.each(function(){
var $this = $(this);
var text = $this.text();
var substr = text.substr(0, 6);
console.log(substr);
$this.text(substr);
});
}
That is basics you should research Google before coming to SO, anyway you can use $(labelStrings).html(subString) or $(labelStrings).text(subString) - they will both change only the inner content between the tags

JavaScript - change font color on certain index

I have this long string and I want a part of it transformed into white colour using only JavaScript.
Example 1:
var string = document.getElementById("subtitle").innerHTML; //returns the string
var i = string.indexOf("("); //returns 80
var j = string.indexOf(")"); //return 93
Example 2: I can get the wanted text but I don't know how to change it white
var string = document.getElementById("subtitle").innerHTML; //returns the string
var i = string.indexOf("(");
var j = string.substring(i, string.indexOf(")")+1); //return the exact string I want to paint white
//j.paintWhite(); how?
I would like to paint all the characters between positions 80 and 93 (or selected as shown in example #2) white. How can I do it?
You need to create an html container for that text where you can specify a style attribute.
also, do not use the variable name string as it is reserved to the language
In order to do that, I would recommend using jQuery, as it is a bit easier.
But if you don't want to, you can do:
var text = document.getElementById("subtitle").innerHTML;
var cut = text.split("(");
var cut2 = cut[1].split(")");
var colored = cut[0] + '<span style="color:#fff;">('+cut2[0]+')</span>'+cut2[1];
document.getElementById("subtitle").innerHTML = colored;
if you assume "transformed into white colour" is doing
<span style="color:#fff">MY_TEXT_HERE</span>
then you could try the following with arrays:
var string = document.getElementById("subtitle").innerHTML;
var arr1 = string.split('(');
var arr2 = arr1[1].split(')');
var finalString = arr1[0] + '<span style="color:#fff">' + arr2[0] + '</span>' + arr2[1];
You must modify the inner Html of the element so it has a text element nested with a defind style.
http://jsfiddle.net/gsexxsbs/
var element = document.getElementById("subtitle");
var htmlText = element.innerHTML;
var i = htmlText.indexOf("(");
var j = htmlText.indexOf(")")+1;
var redText = htmlText.substring(i, j);
element.innerHTML = htmlText.substring(0,i)+"<a style='color:red;'>"+redText+"</a>"+htmlText.substring(j);

Categories