Solved

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

Posted on 2012-03-12
4
210 Views
Last Modified: 2013-02-22
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!
0
Comment
Question by:redds1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 20

Expert Comment

by:Proculopsis
ID: 37710969
<!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>
0
 

Author Comment

by:redds1
ID: 37711767
@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();
                                }

}
0
 

Author Comment

by:redds1
ID: 37711777
can I do this without using jquery
0
 
LVL 20

Accepted Solution

by:
Proculopsis earned 500 total points
ID: 37713761
<!-- You can use a transfrom to inject the values -->
<?xml version='1.0' encoding='ISO-8859-1'?>
<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
    <xsl:template match='/'>

        <xsl:for-each select='root/years/yearl'>

            <input>
                <xsl:attribute name="value">
                    <xsl:value-of select='year' />
                </xsl:attribute>
            </input>            

      </xsl:for-each>

</xsl:template>
</xsl:stylesheet>
0

Featured Post

Application Discovery Service in AWS

In the era of the cloud, customers migrating away from their existing on-premise infrastructure. This requires lots of planning, strategies, and effort to identify their existing resources and determine how best to migrate.  Datacenter migrations happen in four phases -

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question