SitePoint Sponsor

User Tag List

Results 1 to 22 of 22

Thread: getelementsbyid

  1. #1
    SitePoint Member
    Join Date
    Jan 2008
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    getelementsbyid

    i hav a string "string" which has some html code
    "<input type="text" value="val" name="test">.
    i want to get the value of the element contained in the element in the string
    (the element is the input element with name "test" and the value is "val" in the above)

  2. #2
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need to append the HTML code above to the document.body or an document fragment before you can access it.

  3. #3
    SitePoint Member
    Join Date
    Jan 2008
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    do u mean to say that i hav to do some thing like

    document.write(...)
    document.getElementsByName(...)[..]

    if it is what u meant i hav tried it in vain...

  4. #4
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can either use document.write or use DOM methods to append an element to the DOM. Once its is added you can, yes, access it with getElementById, or if a form element: document.forms.formName.fieldName.

  5. #5
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,716
    Mentioned
    103 Post(s)
    Tagged
    4 Thread(s)
    You could use a regular expression to extract the information you desire from the text string.

    To get the value would be something like the following
    value="([^"]+)"

    Regular expressions are inherently greedy, so this one has been told that the returned value should not in itself contain any double quotes.

    Code JavaScript:
    var string = '<input type="text" value="val" name="test">';
    var re = new RegExp('value="(.[^"]+)"');
    value = re.exec(string)[1];

  6. #6
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I assume you want the value that the user entered?

  7. #7
    SitePoint Member
    Join Date
    Jan 2008
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @pmw57
    s i can use....but can u plz sugest an esier method... coz i not sure watz the users input
    i feel that javascript is known for it DOM methods

    @Peprejeria
    yes thatz what i want

  8. #8
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,716
    Mentioned
    103 Post(s)
    Tagged
    4 Thread(s)
    I'm puzzled about why is the html stored in a text string?
    If you're wanting the name of the element that you have stored in the text string, that just means a small change to the previous code.

    Code JavaScript:
    var re = new RegExp('name="(.[^"]+)"');
    name = re.exec(string)[1];

    Then once you have the name you can get it with a standard dom function.

    Code JavaScript:
    value = document.getElementsByName(name);

    If this isn't working for you then you may want to give us a look at what you're doing.

  9. #9
    SitePoint Member
    Join Date
    Jan 2008
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i m sorry for not giving the complete discription of the problem
    i hav a complete html page written in php
    i has some 'name'd elements
    i want to acess that page by ajax and get the 'value's of the 'name'd elements

  10. #10
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why don't you just output the HTML String directly in the HTML code (using PHP instead)?

    You can get the values from the field without using Ajax. What do you want to do once you access the values?

  11. #11
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,716
    Mentioned
    103 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by kranthi117 View Post
    i m sorry for not giving the complete discription of the problem
    i hav a complete html page written in php
    i has some 'name'd elements
    i want to acess that page by ajax and get the 'value's of the 'name'd elements
    Well that's a completely different problem.

    When do you intend for the values to be sent by ajax, and what's going to happen to the values?

  12. #12
    SitePoint Member
    Join Date
    Jan 2008
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i accept that it is a different problem... but it is definitely a problem with javascript DOM methods.

    sorry but u got me wrong again...
    the entire html page(written in php of course) from 'a.php' is sent by 'another' php script 'b.php' to a html file 'c.html' via ajax. the javascript functions are located in 'c.html'. now there is a string stored in a javascript variable "string"

    the varible contains the html code of the entire page 'a.php'

    there is an element with a name 'test' in 'a.php', whose value 'val' i should acess
    Last edited by kranthi117; Jan 7, 2008 at 08:46.

  13. #13
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,716
    Mentioned
    103 Post(s)
    Tagged
    4 Thread(s)
    I think that things are being done the wrong way around here. The php pages should be able to much more easily obtain the information you need and pass it around.

    This appears to be a php question instead.

  14. #14
    SitePoint Member
    Join Date
    Jan 2008
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeh php does provide many features.. thats y i use it
    but the basis of the question lies in getting the html element through DOM...

    and i m sure that itz not a php question

    and through php i think the best thing i can do is use a regexp like the one pmw57 pointed out before(coz php has nothing to do with html DOM as far as i know)
    ..and now this seems to be the only solution

  15. #15
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kranthi117 View Post
    but the basis of the question lies in getting the html element through DOM...
    Code:
    var element = document.getElementById("element_id");

  16. #16
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,716
    Mentioned
    103 Post(s)
    Tagged
    4 Thread(s)
    While we're supposed to stay as far away as possible from innerHTML, here is how to use it to solve your particular problem.

    Code JavaScript:
    <div id="container">
    </div>
    <script type="text/javascript">
        var string = '<input type="text" value="val" name="test">';
        document.getElementById('container').innerHTML = string;
        var input = document.getElementsByName('test');
        // The input element is input[0]
        var inputValue = input[0].value;
    </script>

  17. #17
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why are we supposed to stay as far away as possible from innerHTML?

  18. #18
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,716
    Mentioned
    103 Post(s)
    Tagged
    4 Thread(s)
    Because
    • it is proprietary from Microsoft and not a part of the html specification.
    • you can't use it when you use an XML mime-type
    • it's impure and is similar to wrapping a spaghetti noodle around an orange and calling it lunch
    • there are better ways of doing it

    If you have a php script creating the document that has the string in some javascript, then you can avoid the whole innerHTML mess by having php write the html content itself.

    If the html file is static then the solution is even easier. Take the string out of javascript and have it as normal html in the file itself.

    Both of the above solutions are much cleaner, and remove any possible pitfalls and problems relating to innerHTML.

  19. #19
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    All modern browsers support innerHTML today and it is soon to be a standard, check out the WHATWG specification on this. A proposal has been made for the W3C to adopt this specification.

    There are drawbacks with innerHTML yes, but it can make life easier when you get HTML from an Ajax call for example. It is also much faster than using DOM methods. Especially in Internet Explorer.

    I personally use it only sometimes, since innerHTML can easily be abused.

  20. #20
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,716
    Mentioned
    103 Post(s)
    Tagged
    4 Thread(s)
    It's even faster still when the html file can be created with the html already in place, instead of placing the html content inside a javascript string that will be dumped to the page anyway.

    Creating an html file with a string that contains the html to be used on the page is needless, when the html file can be created with the wanted html already in place on there.

  21. #21
    SitePoint Member
    Join Date
    Jan 2008
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well this was the second i tried just aft string.getElementsByName(...)
    it didnt work then... may b i hav a mistake in some other part of my code...
    and to my surprise it is working now. tnkz for the solution

    While we're supposed to stay as far away as possible from innerHTML, here is how to use it to solve your particular problem.

    and i m surprised why we should stay far from innerHTML
    coz i do most of my work using ajax and i just cant imagine ajax without innerHTML

  22. #22
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kranthi117 View Post
    well this was the second i tried just aft string.getElementsByName(...)
    it didnt work then...
    No, since that method is not a string method.
    Quote Originally Posted by kranthi117 View Post
    and i m surprised why we should stay far from innerHTML
    coz i do most of my work using ajax and i just cant imagine ajax without innerHTML
    See my post above. It can be abused, but can sometimes be great and boost performance. Depends on what you do.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •