0

i have a jsp page... i am adding some content to page dynamically depending upon request parameters (an array will be returned by request) based on this i have to create a drop down. i want to do this on change of another drop down.. so can be done using javascript only but i am unable to use scriptlet in js, is this really possible??

EDIT : i wanna perform some actions on the values retrieved from scriptlet as well

it will be of this sort

function changeMethod(){
    var templateselected = document.getElementById("templateDropDown");
    var versionDropDown = document.getElementById("versionDropDown");
    if ( templateselected.options.selectedIndex != -1)
    {
        var selected=templateselected[templateselected.options.selectedIndex].value;
        removeChildNodes(versionDropDown);
        <% 
        RetrieveTempSecVersions[] lsListOfFiles = (RetrieveTempSecVersions []) request.getAttribute("templateNames") ;
        for (int i=0 ; i < lsListOfFiles[1].getVersionNumber().length ; i++ ) {
            System.out.println("helllooooo");%>
        versionDropDown.innerHTML+='<OPTION VALUE="'+<%=lsListOfFiles[1].getVersionNumber()[i]%>+'">'+<%=lsListOfFiles[1].getVersionNumber()[i]%>+'</OPTION>';
        <%}%>
    }
}

2 Answers 2

1

Yes you can have something like this

function addCombo() {
    var textb = document.getElementById("txtCombo");
    var combo = document.getElementById("combo");

    var option = document.createElement("option");
    <c:forEach var="state" items="${stateList}" varStatus="status">  
    option.text = "${state}";
    option.value = "${state}";
    try {
        combo.add(option, null); //Standard
    }catch(error) {
        combo.add(option); // IE only
    }
    </c:forEach>
    textb.value = "";
} 

Note: I haven't tested this code , this is just a demonstration

Sign up to request clarification or add additional context in comments.

2 Comments

life.java - first of all very nice nic name.. secondly wont we need to create option element again and again?? and do we have if statements also present in jstl??
first of all thanks, secondly this is just a demonstration you will have to modify it according to your need and yes we do have if in jstl check here
0

If the javascript is inline or declared in the same jsp page, there is no problem. Something like:

<script type="text/javascript">
var foo = '${foo}'; // or <%= foo => if you like
</script>

If it is in a separate .js file, then you should serve the .js file through a special servlet.

2 Comments

@varun - what are these "actions". And what stops you from performing them?
@ Bozho - i tried the commented part it didnt worked please refer code provided above

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.