SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict
    Join Date
    Jun 2007
    Posts
    396
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Do you always have to manually define the form name?

    Hi guys, I've got this simple function at the moment:

    Code JAVASCRIPT:
    function newsType () {
    	// grab index number of the selected option
    	index = document.news.type.selectedIndex;
     
    	// get value of the selected option
    	selected = document.news.type.options[index].value;
    	if(selected == 'global') {
    		document.getElementById('description').innerHTML = 'The news item will be visible by all users';
    	} 
    	// etc...
    }

    I've had to define the form name as 'news' manually, and I was wondering if it'd be possible to grab the name when running the function automatically? I want to use this script on other pages/forms so it'd be pretty neat if possible.

    Cheers

  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)
    Assuming that you are calling this function when clicking on a button that belongs to the form
    Code:
    <script type="text/javascript">
    function newsType (form) {
        // grab index number of the selected option
        index = form.type.selectedIndex;
     
        // get value of the selected option
        selected = form.type.options[index].value;
        if(selected == 'global') {
            document.getElementById('description').innerHTML = 'The news item will be visible by all users';
        }
        // etc...
    }
    </script>
    
    <form>
    	<input type="button" onclick="newsType(this.form);">
    </form>

  3. #3
    SitePoint Addict
    Join Date
    Jun 2007
    Posts
    396
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Worked an absolute treat, many thanks

  4. #4
    SitePoint Addict
    Join Date
    Dec 2007
    Posts
    207
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The name attribute is pretty much deprecated and is invalid when using a strict doctype (if I remember correctly).

    http://www.w3.org/TR/html4/interact/...adef-name-FORM

  5. #5
    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)
    It name attribute is there for backwards compatibility. But it is not deprecated.


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
  •