get id by getElementById then set value [closed] - javascript

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I wonder why my code doesn't work, btw I was learning javascript object, so ignore the rest of lines of code.
http://jsfiddle.net/aYK8K/
var p = document.getElementById(p)
p.innerHTML("Hmm Hmm Good");

According to your fiddle you have
<p id="p"></p>
So you will want "p" not p
var p = document.getElementById("p"); // note use of string
p.innerHTML = "Hmm Hmm Good";
To make this less confusing, maybe you should have something like
<p id="hello"></p>
Then
var hello = document.getElementById("hello");
hello.innerHTML = "hello!";
This way you avoid brain dizziness by using different names for your elements, IDs, and JavaScript variables.

its p.innerHTML = "Hmm Hmm Good";
you should change p = document.getElementById(p) to p = document.getElementById('p')
I think you have a confusion with jquery syntax,using jquery you can do like
$('p').html('Hmm Hmm Good');

There's quite a few things wrong with your solution:
For starters, in your object you need to do the following:
var p = document.getElementById('p')
p.innerHTML = 'Hmm Hmm Good';
Next, outside your object you actually need to call the method otherwise nothing will work:
mango.howSweetAmI();
Fiddle: http://jsfiddle.net/KyleMuir/aYK8K/2/

getElementById expects a string
var p = document.getElementById('p')
innerHTML is not a function
p.innerHTML = "Hmm Hmm Good"
good luck!

Related

add a variable in href using javascript [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I am new in javascript i want to add a javascript variable in
i use some javascript like this but it not working
HTML
<ul id="direction"></ul>`
JS
var abc = 'link/';
var cba = 'hello';
document.getElementById('directions').innerHTML = 'Link';`
This code should work, make sure the html id matches the getElementById and use the right quotes as mentioned before.
You want to add a href to a list but you should add a li first, in my example I use a div tag.
var name = 'google';
var ext = '.com';
document.getElementById('direction').innerHTML = 'Link';
A better way to do this would be by using backticks `` < these like so:
var name = 'google';
var ext = '.com';
var link = 'Link';
document.getElementById('direction').innerHTML = `${link}`;
Your code, fixed ID, and quotes:
document.getElementById("direction").innerHTML='Link'
Or maybe better:
var node=document.createElement("A")
node.setAttribute("href","https://"+var1+var2)
document.getElementById("direction").appendChild(node)
I hope that this will help you!
I'd recommend avoiding using innerHTML in most cases! The following accomplishes what you want programmatically without needing to generate HTML strings:
window.onload = () => {
let abc = 'link/';
let cba = 'hello';
let container = document.getElementById('direction');
let link = container.appendChild(document.createElement('a'));
link.setAttribute('href', `https://${abc}${cba}`);
link.appendChild(document.createTextNode('Link'));
};
<ul id="direction"></ul>

Looking for the Javascript of these JQuery lines of code [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm currently learning HTML 5 + javascript, and one of the examples I'm looking at is written in JQuery. I realise that JQuery would be simpler to some people, but as I am just coming to terms with Javascript and no nothing of JQuery, I'm looking to get some lines of code translated from one to the other.
I know what these lines of code does, I'm just looking for it in JavaScript.
var showForm = function() {
if(editMode) {
var transaction = transactions[editMode];
amountField.value = transaction.amount;
if(transaction.type) $('[value='+transaction.type+']').attr('checked', 'true');
if(transaction.cleared) $('#cleared').attr('checked', 'true');
noteField.value = transaction.note;
dateField.value = transaction.date;
$('#time').val(transaction.time);
postcodeField.value = transaction.postcode;
searchField.value = ''
} else {
clearUI();
}
$('#formDiv').show();
$('#toolbar').hide();
$('#tableDiv').hide();
$('#map_div').hide();
$('#sum').hide();
Replace the lines where ever you select the element by ID with the corresponding ID
$('#cleared') ---> document.getElementById('cleared')
you can also use querySelector metod to access the element directly.
var cleared = document.querySelector('#cleared');
To show or hide a element you would need to set the style.display property
// To hide
cleared .style.display = "";
// To show
cleared .style.display = "block";
To get the element based on the attribute would be a bit of javascript..
$('[value='+transaction.type+']')
Where in you would need to iterate over each element and get the attribute of that element and then compare with the value.

How do I cut out a piece of a big string with javascript? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
In javascript, I have a large string like
str="a lot of lines\n"
str+="foo\n"
str+="the stuff I want\n"
str+="bar and a lot more stuff\n"
How do I put just the part:
the stuff I want
between "foo" and "bar" into a new string variable?
Depending on where exactly 'the stuff you want' is in the string you can do a similar approach to this one:
var str="a lot of lines\n";
str+="foo\n";
str+="the stuff I want\n";
str+="bar and a lot more stuff\n";
var stuff = str.split('\n')[2]; // the stuff i want
EDIT:
If you want the stuff between foo and bar, then you can do something like:
var stuff = str.match(/foo((.|\n)*)bar/)[1]; // contains newlines!
var str = "a lot of lines foo the stuff I want bar and a lot more stuff"
str.substring(str.indexOf("foo")+3,str.indexOf("bar") )
var str="a lot of lines\n";
str+="foo\n";
str+="the stuff I want\n";
str+="bar and a lot more stuff\n";
// ------------------------------
var myRegex = /foo(.*)bar/;
if (myRegex.test(str)) {
alert(myRegex[0]);
}
With a regex, it should do the trick.

Get the value of a div [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Helllo, I'm having a div like this:
<div>1+1</div>
How can I get these '1+1' to the javascript and calculate them to '2'?
Thanks in advance!
you can use innerHTML property of element by javascript to extract the value:
document.getElementById("div").innerHTML
and then use eval() method to evaluate the math expression:
var exp=document.getElementById("div").innerHTML;
alert(eval(exp));
You need to somehow identify this div, e.g
<div id="div-with-expression">1+1</div>
And code will be like:
console.log(eval(document.getElementById('div-with-expression').innerText));
You can use eval:
var equation = getElementById(IdOfDiv).innerHTML;
var result = eval(equation);
Well, You could use the Javascript eval() func:
http://www.w3schools.com/jsref/jsref_eval.asp
But, eval functions can introduce malicious code into your environment.
I'm not sure as to what your purpose to start with and what your assumptions are for that input but here's another way to go about it with regex although it's more complicated:
var myRegexp = /(\d+)(\+)(\d+)/;
var match = myRegexp.exec("1+1");
// parseInt(match[0]) == 1 ,as an Int
// match[1] = '+', you could figure what operand this is with 'if else' conditions
// parseInt(match[2]) == 1 ,as an Int

Why does the Javascript replace() function not do anything? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Here's my function:
CSS_Left = '450px';
CSS_Left.replace('px', '');
alert(CSS_Left);
It returns '450px'
Here's an example:
http://jsfiddle.net/D2Mgb/
I want it to replace 'px' with nothing.
In JavaScript, Strings are immutable.
All types except objects define immutable values. Specifically, strings are immutable (unlike in C for instance).
So, str.replace returns a new string. Try this instead:
CSS_Left = CSS_Left.replace('px', '');
Or if you don't want to overwrite the original string, just do it in the alert call
alert(CSS_Left.replace('px', ''));
It's worth noting that .replace can also take a regexp. Though it's not necessary in your case, you could do something like this to achieve the same result
CSS_Left = CSS_left.replace(/px$/, '');
Because .replace() return a new string. If you want to update the old string you should write:
CSS_Left = CSS_Left.replace('px', '');
I am not sure, but the function replace() creates a new variable.
Try this :
var CSS_Left = '450px';
CSS_Left = CSS_Left.replace('px', '');
alert(CSS_Left);
The output of the replace call needs to be reassigned to CSS_Left

Categories

Resources