Avatar of redds1
redds1
 asked on

How to post an array in xslt to a form which can be accesssed by javascript function for validation?

My xsl snippet looks  like this:
<root>
<years>
    <yearl>
        <year>2004</year>
        <period>prior</period>
    </yearl>
    <yearl>
        <year>2005</year>
        <period>prior</period>
    </yearl>
</years>
</root>

I am taking this as input and using xslt to render my page. I want to send all the years to my form.The code looks like:

<form method="POST" action="test.jsp" id="tes">
----
--
 <xsl:for-each select="/ROOT/years/yearl">
     <input type="hidden" name="years[]" id="years[]" value="{/ROOT/years/yearl/@year}" />
--
--
</xsl:for-each>
--
</form>

This is my javascript function which is being called on hitting save button

function alert{

              var year = [document.getElementById('years')];
             var y = year.length  ;                        
                           
                            if (confirm("The year is  " + year[1].value+ " you still want to save?")) {
                                    document.getElementById(test').submit();
                                }

}

In this function I want to get the list of all the years and do some validation based upon that. But this does not seem to work. PLease help!
JavaScriptXMLHTML

Avatar of undefined
Last Comment
Proculopsis

8/22/2022 - Mon
Proculopsis

<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title> - jsFiddle demo by Proculopsis</title>
 
  <script type='text/javascript' src='http://code.jquery.com/jquery-1.7.1.js'></script>
 
<script type='text/javascript'>//<![CDATA[
$(function(){
var years = "";

$("#years\\[\\]").each(function() {
    years += $(this).val();
    years += "\r\n";
});

alert(years);

});//]]>  

</script>

</head>
<body>
  <form method="POST" action="test.jsp" id="tes">
    <input type="hidden" name="years[]" id="years[]" value="2005" />
    <input type="hidden" name="years[]" id="years[]" value="2006" />
    <input type="hidden" name="years[]" id="years[]" value="2007" />
</form>

 </body>
</html>
redds1

ASKER
@Proculopsis: the years are dynamic and they come from the xsl file. How can I send this dynamic array to the form (the values are not hard coded)? In my javascript code, can I use "get element by id" to get the value of this dynamic array?

function alert{

              var year = [document.getElementById('years')];
             var y = year.length  ;                        
                           
                            if (confirm("The year is  " + year[1].value+ " you still want to save?")) {
                                    document.getElementById(test').submit();
                                }

}
redds1

ASKER
can I do this without using jquery
Your help has saved me hundreds of hours of internet surfing.
fblack61
ASKER CERTIFIED SOLUTION
Proculopsis

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.