ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi, I know this is a linux forum, but there are probably experts on js too:
Code:
<!DOCTYPE html>
<html>
<head>
<script>
function getElements()
{
document.getElementsByName("x").value="asshole";
alert(x.length);
}
</script>
</head>
<body>
<input name="x" type="button" value="shit">
<input name="x" type="button" value="fuck">
<input type="button" onclick="getElements()" value="How many elements named 'x'?">
</body>
</html>
sorry for my keywords, those are my foos and bars when i code
For the above code, the length returns correctly, but it doesnt chave the button values for me, why is that?
Thanks,
Ted
sorry for my keywords, those are my foos and bars when i code
You clearly copied the code from W3Schools, yet you bothered to change place-holders?
Quote:
Originally Posted by ted_chou12
For the above code, the length returns correctly,
I doubt that since you have no variable named x.
Quote:
Originally Posted by ted_chou12
but it doesnt chave the button values for me, why is that?
Because you are accessing a non-existent property “value” of whatever getElementsByName returns, which is kind of array. What do you want to happen after you click the button exactly?
You clearly copied the code from W3Schools, yet you bothered to change place-holders?
Yes, this is a modified example from w3schools, but since I am only demostrating my idea, so I thought the source of the code doesnt matter, as long as it shows what I wanted. I did remove the orginal radio buttons and added input buttons instead if you see it in the code.
Quote:
I doubt that since you have no variable named x.
The shit and fxxx buttons are named x, isn't that sufficient? cause I read it that id can only have one on each page, while name can be multiple.
Quote:
Because you are accessing a non-existent property “value” of whatever getElementsByName returns, which is kind of array. What do you want to happen after you click the button exactly?
I want to see both of the buttons change their values. This could be done with ID, but then I would have to give them separate names. I was thinking what if I want something more elegant?
The shit and fxxx buttons are named x, isn't that sufficient? cause I read it that id can only have one on each page, while name can be multiple.
You do not declare a JavaScript variable though. The name of the elements do not matter.
Quote:
Originally Posted by ted_chou12
I want to see both of the buttons change their values. This could be done with ID, but then I would have to give them separate names. I was thinking what if I want something more elegant?
You need to iterate over the array-like object that getElementsByName() returned.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.