Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Using nested element selection with javascript
Old 04-23-2008, 07:32 PM Using nested element selection with javascript
RadGH's Avatar
Skilled Talker

Posts: 76
Name: Radley
Trades: 0
I wonder if this is possible. I can't find anything on google and it doesn't seem to be working through trial and error. I really don't know what I would search for either.

Lets say I have two forms, form one is id "form1", and the first input is "input1"

Would I be able to do something similar to (in CSS) #form1 #input1 { display: none; } with javascript?

What I mean is, use the form ID to select the correct input.

Heres what I'm trying right now but it doesn't seem to work:

form = document.getElementById('form1');
field = document.getElementById('input1');
form.field.value = 'test';
RadGH is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 04-23-2008, 08:39 PM Re: Using nested element selection with javascript
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Well, you can do it:
Code:
document.getElementById('form1').input1.value="test";
It depends if you want to use pure DOM functions, or if using the browsers object models is ok.

But the cleanest method would be to create a simple function to get the DOM reference to the element node:
Code:
/**
 * Returns a Json object with the result of the search, as specified by the user, of the field _field into the form _frm
 * @param String  _frm      The id of the form to look into
 * @param String  _field    The id of the field, contained in the previous form, to find
 * @return JSON             A Json object containing the following structure is returned:
 *                            root['status']: Boolean     Indicate if the field was found or not
 *                            root['elm']   : DomElement  The DOM element reference to the field
 *                            root['msg']   : String      A short description of the cause if the status is false
 */
function getRet(_frm, _field){
  var _trg=document.getElementById(_frm);
  var ret={'status':false,'elm':null,'msg':'Form '+_frm+' not found'};
  if(!_trg || _trg===null){
    ret.msg="The form \""+_frm+"\" was not found";
    return ret;
  }
  else{
    var _f=null;
    for(cptChld=0; cptChld<_trg.childNodes.length;cptChld++){
      _f=_trg.childNodes[cptChld];
      if(_f.id && _f.id.toLowerCase()==_field.toLowerCase()){
        //The field is found, we exit here
        ret.status=true;
        ret.elm=_f;
        ret.msg="Field "+_field+" was found at pos "+cptChld;
        return ret;
      }
    }
    //The field was not found
    ret.msg="Field \""+_field+"\" was not found in form \""+_frm+"\".";
    return ret;
  }
}
And simply use it like this:
HTML Code:
<html>
  <body>
    <form id="frm1">
      <input id="one">
      <input id="two">
      <input id="three">
      <input id="four">
      <input id="five">
    </form>
    <form id="frm2">
      <input id="one">
      <input id="two">
      <input id="three">
      <input id="four">
      <input id="five">
    </form>
    <script type="text/javascript">
function getRet(_frm, _field){
  var _trg=document.getElementById(_frm);
  var ret={'status':false,'elm':null,'msg':'Form '+_frm+' not found'};
  if(!_trg || _trg===null){
    ret.msg="The form \""+_frm+"\" was not found";
    return ret;
  }
  else{
    var _f=null;
    for(cptChld=0; cptChld<_trg.childNodes.length;cptChld++){
      _f=_trg.childNodes[cptChld];
      if(_f.id && _f.id.toLowerCase()==_field.toLowerCase()){
        //The field is found, we exit here
        ret.status=true;
        ret.elm=_f;
        ret.msg="Field "+_field+" was found at pos "+cptChld;
        return ret;
      }
    }
    //The field was not found
    ret.msg="Field \""+_field+"\" was not found in form \""+_frm+"\".";
    return ret;
  }
}
r=getRet('frm2','two');
alert(r.msg);

r=getRet('frm2a','twso');
alert(r.msg);
    </script>
  </body>
</html>
__________________
Only a biker knows why a dog sticks his head out the window.

Last edited by tripy; 04-23-2008 at 08:45 PM..
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 04-24-2008, 05:13 AM Re: Using nested element selection with javascript
ooyes's Avatar
Skilled Talker

Posts: 55
Name: Web Design Company
Location: London
Trades: 0
thanks man its even easier when you use jQuery library

with this plug in http://www.pengoworks.com/workshop/j...eld.plugin.htm

and there you go

HTML Code:
$("input[@name='users']").setValue("test");
__________________

Please login or register to view this content. Registration is FREE
ooyes is offline
Reply With Quote
View Public Profile Visit ooyes's homepage!
 
Old 04-24-2008, 07:17 AM Re: Using nested element selection with javascript
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Well, I admit I'm not a big fan of framework I cannot understand.
I prefer to develop my own little function using pure DOM, than including an 80Ko library for just shortening my code.

Call me grumpy, but I used to have prototype on every site I did, but I dropped this.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Reply     « Reply to Using nested element selection with javascript
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.20230 seconds with 12 queries