Getelementsbyname() Not Working

getElementsByName() not working?

document.getElementsByName() returns a NodeList, so you have to access it by an index: document.getElementsByName('staff_counter')[0] (depending on how many of these you have).

You also have access to a length property to check how many Elements were matched.

getElementsByName not working

Here you go... seems:

  1. onclick="javascript: <--- not necessary - just reference the function name
  2. ShowContentEngineering needs to be set in the window context
  3. You're referencing the "value" attribute of an element that doesn't allow value attributes (h5)

I made it work instead grabbing the innerHTML of the h5
Code

<input type="radio" name="Area" value="Engineering" id="EngineeringCheck" onclick="ShowContentEngineering();" />Engineering

<h5 name="EngineeringAreas"> WHAT THE HECK </h5>
<script>
window.ShowContentEngineering = function() {
alert(document.getElementsByName('EngineeringAreas')[0].innerHTML)
document.getElementById('InformationBlock').style.display = 'block';
}
</script>

Here's a working fiddle: https://jsfiddle.net/mu970a8k/

Using document.getElementsByName() isn't working?

getElementsByName returns an HTMLCollection. You can access the value of the first item like this:

document.getElementsByName("to").item(0).value

Or like this:

document.getElementsByName("to")[0].value

More info:

  • https://developer.mozilla.org/en/DOM/HTMLCollection

document.getElementsByName(q1).disabled = true not working

document.getElementsByName returns a NodeList, which is sort of like an array. You need to loop through it and disable each element.

The NodeList would contain all the elements that have the name q1, and the code below (specifically the .forEach() loop) will disable each element with the name of q1

time = -1;
if (time < 0){ document.getElementById("lose").innerHTML = "O tempo acabou!"; document.getElementById("nextQuestion").style.display = "block" document.getElementsByName("q1").forEach(e => { e.disabled = true; });}
<br><br><input type="submit" name="q1" value="arroz"><input type="submit" name="q1" value="massa"><br><br><input type="submit" name="q1" value="Apanhado em flagrante"><input type="submit" name="q1" value="batata de tremoços"><br><br><br><div id="lose"></div><div id="nextQuestion"></div>

Javascript getElementsByName.value not working

You have mentioned Wrong id

alert(document.getElementById("name").value);

if you want to use name attribute then

alert(document.getElementsByName("username")[0].value);

Updates:

input type="text" id="name" name="username"  

id is different from name

Cannot getElementsByName of a website until I inspect it

In Chrome, it has a nice feature when using the console if you select an iFrame it will target this for the document. It looks like in Firefox is does not have this feature.

If your wanting to do this with code you will basically need to do the same thing as Chrome.

Also if you always want to select the same thing, you may also want to do window.top to also make sure you always starting from the same place, in case inside the inspect you select another document.

So using this idea, this should do the trick.

window.top    //first start from the main top window
.document.querySelector('iframe') //now select the iFrame
.contentDocument.querySelector('[name=username]') //finally do our selector

Result:

Sample Image

Get elements by name not working when targeting inside ID .getElementsbyTagName is not a function error

you could do

var foo = document.forms['myForm']['hello'];

you can also use dot notation for easier coding. getting suburb's value:

var input = document.forms.myForm.suburb,
suburbValue = input.value;

but as @sundeep suggests in the comments, .getElementsByName() cannot be chained to a document.getElementById('form')

Why isn't get Elements By Name working?

This is an optimized script that makes use of proper page load wait and then css selectors. CSS selectors are a faster, more flexible, way of matching on elements.
I think it makes for nice clean reading as well.

The [x=y] e.g. [value=list] are attribute = value selectors. The input is a type selector. These selectors are applied via querySelector method of HTMLDocument object and return the first match in the DOM for the specified css selector.

Option Explicit

'VBE > Tools > References:
' Microsoft Internet Controls
Public Sub EnterInfo()
Dim ie As New InternetExplorer
Const VERB As String = "querer"

With ie
.Visible = True
.Navigate2 "http://www.conjugation.org/"

While .Busy Or .readyState < 4: DoEvents: Wend

With .document
.querySelector("input").Value = VERB 'first input tag element
.querySelector("[value=list]").Click '< first element with value attribute having value of list
.querySelector("[value=Conjugate]").Click '< first element with value attribute having value of Conjugate
End With

Stop '<= delete me later
.Quit
End With
End Sub

Exploring css selectors in your browser (Chrome shown):

Sample Image


Practicing css selectors:

https://flukeout.github.io/



Related Topics



Leave a reply



Submit