How to get the id of an item in an array? [closed] - javascript

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 8 years ago.
Improve this question
var a = document.getElementById('abc');
alert(a.id) // it works.
var b = document.getElementsByClassName('qwe');
alert(b[0].id); // this is not working. returns only empty space : ''
I don't know why this happening.
a is object, and b is array object. and b[0] is object property. and also object.
Is there any one who can explain this happening's reason and solution clearly?
ps. sorry for miss 'document'. I missed it when I write this question. but in the source code, I didn't miss it.
<html>
<div id="abc" class="qwe"></div>
<div id="a1" class="qwe"></div>
<div id="a2" class="qwe"></div>
<div id="a3" class="qwe"></div>

if you write document before getElementById it should work fine .
Here is jsBin example JSBIN
var a = document.getElementById('abc');
alert(a.id) // it works.
var b = document.getElementsByClassName('qwe');
alert(b[0].id); // this is not working. returns only empty space : ''

JSFiddle
You forget to use document before getElementById and getElementsByClassName, other wise its working fine. check out above jsfidlle link

Related

How can i navigate to below class [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 2 years ago.
Improve this question
I am trying to access the below tag class="topbar"
<div id="swagger-ui">
<section data-reactroot="" class="swagger-ui swagger-container">
<div class="topbar">
<div class="wrapper">
<div class="topbar-wrapper">
I have tried this:
var x=document.getElementById("swagger-ui");
var y=x.getElementByClassName("swagger-ui swagger-container");
var z=y.getElementByClassName("topbar");
Also,
How can I set value in js? I have to set the value for input type text
<div class="wrapper"><label>Value:</label><section class=""><input type="text"></section></div>
You can do something like the following:
var topbar = document.getElementsByClassName('topbar');
or
var topbar = document.querySelector('.topbar');
try to use querySelector
var x=document.querySelector("#swagger-ui");
var c=document.querySelector(".swagger-ui");
var y=document.querySelector(".swagger-container");
You're code is almost perfect, but you need to spell getElementByClassName correctly, and add an indexer for getElementsByClassName. Try adding [0] after each getElementsByClassName function. Like this: getElementsByClassName[0]

Can't remove a list item? [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 6 years ago.
Improve this question
I am trying to remove a list item.
I add an item in like so:
$('#btnAdd').click(function(){
var text = $('#item').val();
if(text.length){
$('<li />',{html: text}).appendTo('ul.justList')
}
});
then remove it like so:
$('#btnRemove').click(function(){
$("#justList li:last-child").remove();
});
Why does the last list item not remove? Any suggestions? Thanks so much!
I think the problem may be in your punctuation. Note the difference in punctuation between "justList" in .appendTo('ul.justList') and in $("#justList li:last-child").
ul.justList says, "Look for a <ul> with the class of 'justList'," or <ul class="justList">.
#justList says, "Look for any element with the ID of 'justList'," or <any-element id="justList">.
Classes and IDs are different, so make sure you're using the correct punctuation: . for a class, and # for an ID.
Based on your add statement I would guess that you need a remove statement like:
$('#btnRemove').click(function(){
$("ul.justList li:last-child").remove();
});
If your add statement is working then this should be accessing the same ul
Please use . instead of # while remove as well
$('#btnRemove').click(function(){
$(".justList li:last-child").remove();
});

Javascript To do list - whats wrong in my code? [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 6 years ago.
Improve this question
I have been watching a tutorial about how to make a to do list and so far my code is IDENTICAL to the one in the tutorial but still doesn't work-Ive been starring at the code for forever but can't figure out why..
my html (I don't have to include etc because I was writing my code on codepen(javascript,html and css are already being combined):
<body>
<h1>To Do List</h1>
<p><button id="add">Add</button></p>
<ul id="todoList"></ul>
</body>
Javascript:
function addNewItem(){
var listItem = document.createElement ("li");
listItem.innerText = "hello";
var list = ducument.getElementById("todoList");
list.appendChild(listItem);
}
var btnNew = document.getElementById("add");
btnNew.onclick = addNewItem;
In the tutorial the same code works and when you click the add button "hello" is being displayed and in my code nothing happens when i click on the add button..
You have a typo on this line:
var list = ducument.getElementById("todoList");
It's document, not ducument.
See this working fiddle: https://jsfiddle.net/omgtoblerone/h2sL1vu8/
In most browsers, if you hit F12, you'll see an error console. If you had looked at the console, you would've seen the following error:
Uncaught ReferenceError: ducument is not defined
Easy fix!

What is causing my jQuery functions not run? [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 9 years ago.
Improve this question
I can't figure out why the jQuery functions will not work with the html here
JS Fiddle: http://jsfiddle.net/JonaTheApprentice/E89rg/
function addItem() {
$('#list').append('<li><input type="checkbox" /> item</li>');
}
function removeItem() {
$('#list').children().filter(function () {
return this.firstChild.checked;
}).remove();
}
I don't see an element with id="list", that's what #list refers to.
I think you mean #items-listed. Changing that, and it works
I would get the value from the input id. I would also assign a click event for cleaner coding. Here is an example of your fiddle adding an item. Removing an item is just the reversal.
DEMO http://jsfiddle.net/E89rg/5/
$('#Enter').click(function(){
var Val = $('#item').val();
$('#items-listed').append('<li><input type="checkbox" />'+Val+'</li>');
});

$("divId").text();....is not working [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 9 years ago.
Improve this question
I have a contenteditable div inside jquery tab and I am trying to get the content using a
button. When I click on button a javaScript function is getting called,
inside the function I am trying to grt the content
HTML code
<div id="tab1" contenteditable="true">
<div>line no one</div>
<div>line no one</div>
</div>
JavaScript function
alert($("tab1").text());
alert($("tab1").html());
error
For text(); I am not getting any thing but for html(); it is showing Undefined
I am not able to guess whats wrong with the code...
Any suggestions...
You need to use ID indication "#"
alert($("#tab1").text());
// ^here
DEMO
See you have missed # notation for id selector and make sure to have it wrapped in $(function(){...}) doc ready handler:
$(function(){
alert($("#tab1").text());
alert($("#tab1").html());
});
jQuery expects a selector, not an id. "tab1" is a type selector, it matches <tab1> elements (which do not exist in HTML).
You can use an ID selector though: $("#tab1").
Are you using jquery?
Does it load proprely?
try this
alert($("#tab1").text());
alert($("#tab1").html());
you have to tell jquery that you are looking for an id with "#"

Categories

Resources