IF ElementID value then change another ElementID value [closed] - javascript

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
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.
Improve this question
I'm trying to lookup a field's value, if it equals '1', then put the value '1' in different field, if not put a '0'.
I'm not sure why this isn't working, can anyone help?
<input type="text" name="_1_1_33_1_id" value="" onchange="checkLineManager();">
<input class="valueeditable" type="text" name="_1_1_118_1" id="_1_1_118_1" value="" >
Javascript:
function checkLineManager() {
if (document.getElementsByName('_1_1_33_1_id').value == '1') {
document.getElementById('_1_1_118_1').value = '1';
} else {
document.getElementById('_1_1_118_1').value = '0';
}
}
http://jsfiddle.net/nbren007/o9xp0efy/

Note the plural use of "elements" in the following line:
if (document.getElementsByName('_1_1_33_1_id').value == '1') {
This doesn't return an element, it returns a node list.
// To confirm that
alert(document.getElementsByName('_1_1_33_1_id').toString());
So you need to use:
if (document.getElementsByName('_1_1_33_1_id')[0].value == '1') {
There are other ways of accessing the element as well. Most notably through the form element approach.

The hint is in the method: getElementsByName returns more than one element - it returns an array of matching elements.
You need to use array notation to select the element from the array.
Change:
document.getElementsByName('_1_1_33_1_id')
To:
document.getElementsByName('_1_1_33_1_id')[0]
if (document.getElementsByName('_1_1_33_1_id')[0].value == '1') {
document.getElementById('_1_1_118_1').value = '1';
} else {
document.getElementById('_1_1_118_1').value = '0';
}
Or even neater, use a ternary statement:
document.getElementById('_1_1_118_1').value =
document.getElementsByName('_1_1_33_1_id')[0].value == 1 ? '1' : '0';

Related

How to check if a number is NOT a property in a javascript object [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I wanna check if a number is not a property in an object and if is not add it to the object as a property.
var obj{}
var arr=[1,2,3,4]
I´ve tried if(! number in object ) and if(! object.hasOwnProperty(number)) but didn´t work, and in both cases, I also tried to make the number become a string doing number.toString(), and nothing happened.
ar.forEach(number=>{
if(!(number in obj)){ //or number.toString()
})
ar.forEach(number=>{
if(! obj.hasOwnProperty(number)){ //or number.toString()
})
Your code actually works once you fix the typos:
var obj = {3: 'hey'}
var arr = [1, 2, 3, 4]
arr.forEach(number => {
if (!(number in obj)) {
console.log(number + ' is not in obj')
}
}
)
Object.values(your_object) gives you an array with all the values in your object.
Then iterate trough that array and check if it contains your number.

Argument value is not getting passed in 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 3 years ago.
Improve this question
I'm passing some value on a onclick=function('') function but the value is not getting passed
while ($row = mysqli_fetch_array($result)) {
$id = $row['id'];
echo '<img class="rmv-img" src="img/bin-with-lid.png">';
}
function DeleteUser(deleteid){
var conf = confirm("deleteid");
if (conf == true) {
$.ajax({
url: "submit/graduation-submit.php",
type :'POST',
data: { deleteid : deleteid},
});
}
}
Here in the parameter I should get the value of id but on the alert side, this is showing deleteid itself
The problem is only the double quotes:
var conf = confirm(deleteid);
This will show the value of the id in the alert.
You are not passing argument to your confirm box,
var conf = confirm("deleteid");
Passing "deleteid" is nothing but a string and not an argument. If you want to show deleteid on confirm box then do this,
var conf = confirm("Are you sure, you want to delete "+deleteid);

can not parse getElementById [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have this function which produces the correct value when run, but I am having a hell of a time displaying the results.
Here is the JS which is calculated onChange in a form I am trying to display the resulting value elsewhere on the form. The alert displays the correct value but my id remains blank.
Thanks in advance for taking a look
function calculate_mtow1() {
togw_n = 0;
togw = $('#togw').val();
if (togw != '' && togw != 0 && togw != 'Nan') {
var togw = togw.replace(",", "");
togw_n = togw;
}
burn_n = 0;
burn = $('#burn').val();
if (burn != '' && burn !=0 && burn != 'Nan') {
var burn = burn.replace(",", "");
burn_n = burn;
}
var mtow1 = parseInt(togw_n) + parseInt(burn_n);
$('#mtow1').val(mtow1);
document.getElementById('mtow1');
alert(mtow1);
}
<td>TOW + Fuel Burn =<span id="mtow1"></span></td>
Your code is getting the element with getElementById but then not doing anything with it. You need to assign the result of getElementById to something, or call methods on it on the same line. If your goal is to put the value of mtow1 into your <span>, try doing this:
// Solution 1
var spanElement = document.getElementById("mtow1");
spanElement.innerHtml = mtow1;
Alternatively, perhaps you were trying to display the value of mtow1 by using this jQuery:
$('#mtow1').val(mtow1);
That doesn't do what you think it does. It changes the "value" attribute of the span to the value of mtow1, but that change isn't visible to the user. It's the same as writing this as your HTML:
<td>TOW + Fuel Burn =<span id="mtow1" value="valueofmtow1"></span></td>
If you want to use jQuery instead of the getElementById method I posted above, you could do this:
// Solution 2
$('#mtow1').html(mtow1);
You don't need to do both. Either solution will work on its own.

JQuery syntax - remove() won't work on element [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I need to remove a particular element within a container if it exists. The below code alerts to tell me the element exists but for whatever reason, does not remove and oes not provide any error messages.
var second_page_item = $('DIV#discount_0 > .element .first_static .second_page .isotope-item');
if($(second_page_item).length == 0) {
alert('it here');
$(second_page_item).remove(); //WHY DOESNT IT REMOVE?
}
So if it ....
if($(second_page_item).length == 0) {
.... has no length, and doesn't exist, remove it
$(second_page_item).remove();
makes perfect sense, but there's nothing to remove ?
second_page_item is already a jQuery object, so there's no reason to run jQuery on it again. Also, a length of 0 would mean it doesn't exist. This is a more logical approach to your version of the code.
var second_page_item = $('DIV#discount_0 > .element .first_static .second_page .isotope-item');
if ( second_page_item.length > 0 ) {
second_page_item.length.remove();
}
However, you can simply do.
$('DIV#discount_0 > .element .first_static .second_page .isotope-item').remove();
It should work:
if($(second_page_item).length > 0) {
alert('it here');
$(second_page_item).remove();
}

Adding an ID to the child of an element [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 have some HTML code that I am unable to edit without using javascript. The DIV I am trying to access is the child of another that has an ID:
<div id="contentcolumn">
<div class ="content content_gray"></div>
<div class ="forum_tabs"</div>
</div>
The DIV I am trying to access is the one with the class of "content content_gray". I do not want to access it using the class because other elements have this class. I must access it by access the first-child of the DIV with the ID of "contentcolumn".
I must access it by access the first-child of the DIV with the ID of "contentcolumn".
You mean like this?
var elm = document.getElementById("contentcolumn").firstElementChild;
You've said it's the first child, so I didn't bother to check className, but of course you can if you like. You'd add that check by doing:
if (elm.className.match(/\bcontent\b/) && elm.className.match(\bcontent_gray\b)) {
// ...
}
Note that not all browsers have firstElementChild. (I don't recall for sure whether IE8 does, for instance.) For those that don't, a simple loop does it (and works even on those that have it):
var elm = document.getElementById("contentcolumn").firstChild;
while (elm && elm.nodeType !== 1) {
elm = elm.nextSibling;
}
To you could have a little utility library you use:
function firstElementChild(parent) {
var node = parent && parent.firstElementChild;
if (!node && parent) {
node = parent.firstChild;
while (node && node.nodeType !== 1) {
node = node.nextSibling;
}
}
return node;
}
You asked below how to set the element's id once you have the element. That part's easy:
elm.id = "value";
check the element.className of each document.getElementById("contentcolumn").getElementsByTagName("div")
or else, jQuery("#id > .class") looks simpler to me

Categories