XHTML strict, name vs. id

This is a discussion on "XHTML strict, name vs. id" within the JavaScript Forum section. This forum, and the thread "XHTML strict, name vs. id are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > JavaScript Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Feb 27th, 2008, 15:32
masonbarge's Avatar
Highly Reputable Member
Join Date: Jan 2006
Location: Atlanta GA
Posts: 631
Thanks: 0
Thanked 0 Times in 0 Posts
XHTML strict, name vs. id

I have a jump navigation box that works great
Code: Select all
<form id="menu" name="menu" class="jump" action="jump.php" method="post">
<fieldset class="noborder">
<noscript>
<div><input class="gobutton" type="submit" value="Go" />
<input type="hidden" name="submitted" value="cassio" /></div>
</noscript>
<select class="fr jumpselect" name="list" size="1" 
 onchange="if (document.menu.list.selectedIndex != 0) 
window.open(url = document.menu.list.options[document.menu.list.selectedIndex].value,'_self'); 
else alert('Please choose from menu.')">
<option selected="selected" value="#">Other Pages</option>
<option value="http://collierhills.net/path.php">PATH</option>
... more options ...
</select>
</fieldset></form>
My problem is that I can't get it to work without the 'name="menu"' attribute which means, of course, it won't validate in XHTML strict, where the name attribute is fully deprecated for <form>.

I've tried using only id="menu" with the following code, but it didn't work:
Code: Select all
onchange="if (document.form['menu'].list.selectedIndex != 0) 
window.open
 (url = document.form['menu'].list.options[document.form['menu'].list.selectedIndex].value,'_self'); 
else alert('Please choose from menu.')"
Does anyone know how to fix this so it will work without a name attribute in the form tag?

Last edited by masonbarge; Feb 27th, 2008 at 15:34. Reason: eliminate scrollbars
Reply With Quote

  #2 (permalink)  
Old Feb 27th, 2008, 17:09
SuperMember

SuperMember
Join Date: May 2007
Location: UK
Age: 27
Posts: 1,111
Thanks: 0
Thanked 0 Times in 0 Posts
Re: XHTML strict, name vs. id

That's some pretty weird javascript you have there! You need to use a different way to reference your HTML elements.

The simplest way is to give everything an ID. For example, <select id="list">.

Then use document.getElementById to refer to it. For example, onchange="if (document.getElementById("list").selectedIndex != 0)

Not sure about that last bit (selectedIndex).
Reply With Quote
  #3 (permalink)  
Old Feb 27th, 2008, 21:09
masonbarge's Avatar
Highly Reputable Member
Join Date: Jan 2006
Location: Atlanta GA
Posts: 631
Thanks: 0
Thanked 0 Times in 0 Posts
Re: XHTML strict, name vs. id

Thanks for the response.

I found a really elegant, long-term solution at http://abeautifulsite.net/notebook.php?article=28

Basically, you create a function that turns any "select" tag, which has an id tag, into a javascript jump link. Then you just put it in an external file.

This is nice, since you still have "name" attribute available for "select" tags in XHTML 1.0 Strict. And if you need the id tag in a select for something else, even a bare-amateur like myself can change the javascript.

Here's the function:
Code: Select all
function initJumpMenus() {
    var selectElements = document.getElementsByTagName("select");
    for( i = 0; i < selectElements.length; i++ ) {
        if( selectElements[i].className == "jumpmenu" && document.getElementById(selectElements[i].id) != "" ) {
            jumpmenu = document.getElementById(selectElements[i].id);
            jumpmenu.onchange = function() {
                if( this.options[this.selectedIndex].value != '' ) {
                    location.href=this.options[this.selectedIndex].value;
                }
            }
        }
    }
}
Then all you have to do is initialize it
Code: Select all
window.onload = function() {
    initJumpMenus();
}
Reply With Quote
  #4 (permalink)  
Old Feb 27th, 2008, 21:18
masonbarge's Avatar
Highly Reputable Member
Join Date: Jan 2006
Location: Atlanta GA
Posts: 631
Thanks: 0
Thanked 0 Times in 0 Posts
Re: XHTML strict, name vs. id

PS -
I only know what selectedIndex is from trying to get this jump menu working right, but it's not a bad element to know.

"selectedIndex - The number (base 0) of the item that is selected in the select list"

It would be a nice way to do a css-switch from a dropdown list, for example.

All the usages I have seen have used some sort of "if" statement so that no action occurs if the first list item is selected, i.e. value=0, which is the base value of the element.
Reply With Quote
Reply

Thread Tools

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 On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Strict XHTML - Displaying RSS feeds grandadbob Web Page Design 2 Mar 7th, 2008 09:10
Strict Vs Transitional - Problems in Firefox !! slimboyfatz32 Web Page Design 11 Feb 19th, 2008 09:46
Problem validating "Strict" with forms Craigj1303 Web Page Design 2 Jan 22nd, 2008 15:01
Strict XHTML validation error : "tabindex" newoptical Web Page Design 1 Aug 24th, 2007 16:21
Form fails strict XHTML validation newoptical Web Page Design 10 Jun 7th, 2007 12:08


All times are GMT. The time now is 01:18.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43