Dear Friends,
we are facing a problem with alt attribute in Java script.While retrieving alt attribute in IE its working fine but in FireFox we are getting as undefined.Please give solution for retrieving the alt attribute in firefox.

please find the below code for how i am retrieving...

<html>
<head>
<script type="text/javascript">

 window.onload = function()
 {


 var els= document.forms[0].elements;
 var compcode ="";
 for(var i= 0; i < els.length; i++)
 {
  if (els[i].id!="")
      {
   alert(els[i].id);
   alert(els[i].title);
   alert(els[i].alt);
  }

 }
}

</script>
</head>
<form action="#">
 <select name="test" id="testing" alt="test|001" title="test|ddd">
 </select>
</form>
</html>

The above code is working fine in IE but not in firefox

Cheers

Siva kumar

Recommended Answers

All 2 Replies

Hi,

try this instead:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<title>www.daniweb.com</title>
<script type="text/javascript">
<!--
var d = document;
window.onload = function() {
var els = ( "testform" in d ) ? d.testform : d.getElementById( "testform" );
var attr = [ "id", "title", "alt" ];
   if ( els ) { 
      els = els["testing"];
      var col = "\n";
      for ( var i = 0; i < attr.length; i++ ) {
         if ( els.getAttribute( attr[ i ] )) {
            col += attr[ i ] + " : " + els.getAttribute( attr[ i ] ) + "\n";
            continue;
         }
      } return alert( col );
   } alert( "Unsupported script, please update your browser" );
   return false
}
// -->
</script>
</head>
<body>
<div>
<form id="testform" name="testform" action="#" onsubmit="return false;">
<div>
<!-- Using ( ALT ) Attribute in a select tag is NOT RECOMMENDED in any of the W3C Standard (x)HTML document templates'.

This document will output 1 error: ( Attribute "ALT" is not a valid attribute ) -

and will not validate, if you will attempt to verify these document with W3C markup validation tool.
-->

<select id="testing" name="testing" alt="test|001" title="test|ddd" size="1">
<option value="alt attribute">ALT Attribute</option>
</select>
</div>
</form>
</div>
</body>
 </html>

Hi essential,

Thanks for your reply.
This code really helps :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.