I'm trying to make the text stored in the variable sendSpecialChat uppercase, but I can't find out why it doesn't work. This is not actually what I'm trying to do, but I simplified the code:
var sendSpecialChat = hi;
document.writeIn(sendSpecialChat.toUpperCase());
document.getElementById('print').innerHTML = "sendSpecialChat";
<p id="print"></p>
Although, it doesn't work. What's wrong with my code?
sendSpecialChat.toUpperCase() will give you an uppercase version of the string. You have a few problems with your code, you need to use quotes(") around strings, but not around variable names.
var sendSpecialChat = "hi";
document.getElementById('print').innerHTML = sendSpecialChat.toUpperCase();
<p id="print"></p>
Related
I would like to add a function in an onclick with 3 parameters: int, int, string. The string parameter that contains an HTML and other quotations. Would it be possible?. I saw it here pass string but it is not working on my case.
<p onclick="myfunction(0,3,'<b">FirstName =
\"Dondellx\"\n<b>LastName</b> = \"Batacx\"\n<b>path</b> =
\"xxxpath\""');dondell
</p>
This function that contains the String with many \\ characters are actually from a JSON file which I put in a "p" tag in a loop when reading the JSON.
This is what i am doing to get the JSON item and put to the string param;
//This is a for loop
<p onclick="myfunction(0,3,'dataFromJSon[0]')"></p>
<p onclick="myfunction(0,3,'dataFromJSon[1]')"></p>
<p onclick="myfunction(0,3,'dataFromJSon[2]')"></p>
This is the text editor to enter my Text.
As you can see, I have quotations in the textArea, so that is why i need to save it with qoutations. :
This is the JSON:
Your html is misformed indeed, so I created a simpler demo. Hopefully it'll still be useful as I use an object to pass the html code as well as the other parameters.
var object = {a:0,b:3,c:'<b>FirstName=</b>Dondellx'};
function myfunction(obj){
document.getElementById("out").innerHTML=obj.c;
}
<p onclick="myfunction(object)">click</p>
<div id="out"></div>
This is certainly because there are so many syntax error in you HTML and JavaScript code.
Not correctly escape quote, not escape line break inside a javascript string declaration, HTML tag not properly close, attribute value not correctly defined...
Maybe try this (I have implement a fake myfunction for exemple and replace \n by <br/>, the line break not make sense in HTML).
function myfunction(a,b,str){
var row = document.createElement('div');
row.innerHTML = str;
document.body.appendChild(row);
}
<p onclick="myfunction(0,3,'<b>FirstName = "Dondellx"<br/><b>LastName</b> = "Batacx"<br/><b>path</b> = "xxxpath"');">dondell
</p>
I would like to know if the following line is the correct way to take html and put it into a Javascript var as numeric value?
var thePoolHeatingFeeRounded = Number(document.getElementsById("priceDisplayPoolHeating").innerHTML);
Nevermind that the variable name has the word 'rounded' in it. The value that I am trying to put into this var is a 2 point float that has already been rounded and it exist as html.
Below is the line of HTML code that is referenced by getElementsById...
$<div id="priceDisplayPoolHeating" class="priceDisplay">8.00</div>
Any input is greatly appreciated.
Try this instead:
var thePoolHeatingFeeRounded = Number(document.getElementById("priceDisplayPoolHeating").innerHTML);
You were calling getElementsById which is not correct (it is not plural), I changed it to getElementById
Tip: if you need to check whether the Number is valid you can use !isNaN(thePoolHeatingFeeRounded) or use a trick to turn it into a default number (such as 0 or -1) like this:
var thePoolHeatingFeeRounded = Number(document.getElementById("priceDisplayPoolHeating").innerHTML) || 0;
You can also use parseFloat():
var thePoolHeatingFeeRounded = parseFloat(document.getElementById("priceDisplayPoolHeating").innerHTML) || 0;
You are very close. Supposed to be getElementById, not Elements. I created a little code to show you how it works.
Here's what the code looks like in this website's code displayer:
function displayText(){
var thePoolHeatingFeeRounded = Number(document.getElementById("priceDisplayPoolHeating").innerHTML)
alert(thePoolHeatingFeeRounded);
}
<div id="priceDisplayPoolHeating">8.01</div><input type="button" onclick="displayText()" value="Display the innerHTML of Pool Heating">
I have a string:
Name1<br/>Name2<br/>Name3
Im looking to get a choice selector or an array with just the Names as values. I know you can get just the text of a string, but I cant figure out a way separate them. This list changes so I cant hard code the names in.
I cannot find any code nor do I have anything yet.
Use the split function:
var text = "Name1<br/>Name2<br/>Name3";
var list = text.split("<br/>");
This is easily accomplished using JavaScript built-in split().
var input_s = "Name1<br />Name2<br />Name3";
var input_r = input_s.split("<br />");
I'm trying to parse a JSON string and I can't get it to work because of illegal chracters - which I cannot find...
Here is what I have:
make = function (el) {
var config = el.getAttribute("data-config");
console.log(config);
var dyn = $.parseJSON(config)
console.log(dyn);
}
var a= document.createElement("Star Icon");
console.log(a);
make(a);
I'm not really sure how to correctly unescape the JSON in my original string "a", so that it works.
Question_:
Which quotation marks do I need to escape to get this to work?
Thanks!
EDIT:
Ok. I figured it out using Jquery (I'd prefer Javascript-only though). This works:
make = function (el) {
var config = el.attr("data-config");
console.log(config);
var dyn = $.parseJSON(config)
console.log(dyn);
}
var c = $('<a href="#" class="template" data-config=\'{"role":"button","iconpos":"left","icon":"star","corners":"false","shadow":"false", "iconshadow":"false", "theme":"a","class":"test", "href":"index.html","text":"Star Icon", "mini":"true", "inline":"true"}\'>Star Icon</a>')
console.log(c);
make(c);
So escaping the start/end quotations of the JSON string seems to do the trick. The actual problem was that I can not use document.createElement with a full string. I can only create the element document.createElement(a) and then set innerHTML. Need to look into this some more.
If someone can tell me a Javascript-only way how to do this, please let me know.
Thanks!
Strings and object keys in JSON must be double quoted. Double quotes in attributes are not valid, so you'll need to escape them with ".
Also, you probably want to use booleans true/false instead of strings "true"/"false".
var a = document.createElement('Star Icon');
Notice this is completely unreadable and #millimoose's suggestion about just setting the attribute afterwards will make this much easier to deal with in the long run.
I found this code on another question, but am not sure how to use it:
var text = document.getElementById("text").innerHTML;
text = text.replace(/"z"/g,
function(n){ return ++n });
document.write(text);
It's exactly what I need, but I'm not sure how to pass a number to the function. Essentially, I have a large list of csv (comma delimited) items and need to look for each occurrence of "z" and replace it with a number incremented by one. The code is here, but I'm just not knowledgeable yet to know how to use it.
I've placed my text witch needs to be replaced inside of a div with an id of text. Any ideas?
Define a variable n before that code:
var n = 0;
Remove the n argument from the function, so you have
function(){ return ++n });
The n then refers to the variable you defined and the function requires no arguments.
The argument n was the matched text in the question you referenced. In this case, that text is irrelevant, as it is always z.
document.write is generally not a good idea. It would be better to replace it by assigning to the innerHTML of an element of your choice.
The value passed into the function in the variable n is what's been found in the string. That's a number in the question you've referenced but here it's text (the letter 'z'). Instead of using the text that was found you can just create a global variable and use that instead:
var text = document.getElementById("text").innerHTML;
var num = 0;
text = text.replace(/z/g, function(n){ return ++num });
document.write(text);
(Also note that you need to get rid of the quotes around the 'z' character).
If I read the code correctly. It takes the inner html from a single element with id=text.
e.g the span elements below:
<div id="text">
<span id="exzzzample">maybe CSV data is here z</span>
<span>More CSV Data z z z z</span>
</div>
It then replaces each z with a number that keeps incrementing. This would be:
<span id="ex123ample">maybe CSV data is here 4</span>
<span>More CSV Data 5 6 7 8</span>
The starting value of n can be set outside of this (the line before your code starts) with:
var n = 567; // An arbitrary starting value.
Note that any "z" in the attributes of contained elements will also be replaced as per my example. "z" should at least be very unique if you decide to solve your problem this way.
you need to fix the id of the element you're replacing the text into. This works:
<script>
var n = 0;
function replaceText() {
var text = document.getElementById("text").innerHTML;
text = text.replace(/"z"/g, function(){ return ++n });
// Changed to be placed here as suggested
document.getElementById("text").innerHTML = text;
}
</script>
<div id="text">
"6","z","AAA 101 - College 101:Student Experience","Introduces students to college culture and prepares...","1"
</div>
<input type="button" value="Find" onClick="replaceText();">