LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-29-2013, 07:01 PM   #1
ted_chou12
Member
 
Registered: Aug 2010
Location: Zhongli, Taoyuan
Distribution: slackware, windows, debian (armv4l GNU/Linux)
Posts: 431
Blog Entries: 32

Rep: Reputation: 3
javascript getelemenetbyname and value


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
 
Old 01-30-2013, 06:47 AM   #2
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
Quote:
Originally Posted by ted_chou12 View Post
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 View Post
For the above code, the length returns correctly,
I doubt that since you have no variable named x.

Quote:
Originally Posted by ted_chou12 View Post
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?
 
Old 01-30-2013, 01:02 PM   #3
ted_chou12
Member
 
Registered: Aug 2010
Location: Zhongli, Taoyuan
Distribution: slackware, windows, debian (armv4l GNU/Linux)
Posts: 431

Original Poster
Blog Entries: 32

Rep: Reputation: 3
Quote:
Originally Posted by mina86 View Post
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?

Btw, you can check it on this page, just copy and paste it, the length does return correctly.
http://www.w3schools.com/jsref/tryit...getelementbyid

Thanks,
TEd

Last edited by ted_chou12; 01-30-2013 at 01:06 PM.
 
Old 01-31-2013, 07:05 AM   #4
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
Quote:
Originally Posted by ted_chou12 View Post
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 View Post
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.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
javascript katea Programming 11 02-16-2011 06:43 PM
javascript paul62 Linspire/Freespire 3 11-29-2009 07:02 PM
Looking for Textbrowser with JavaScript Support or Elink Javascript Support rohezal Linux - Software 4 09-01-2009 01:02 PM
Ubuntu Edgy/Firefox 2/Javascript - Firefox closes accessing websites with Javascript Interdictor Ubuntu 8 11-02-2006 11:58 AM
Javascript help darin3200 General 4 09-03-2005 11:58 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:41 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration