YOYO HONEY SINGH
YOYO HONEY SINGH

Reputation: 63

How to pass javascript variable to servlet

I am using selectable jquery in my jsp page. On submitting the form, the string should pass as parameter to servlet.

$(function() {
    $("#selectable").selectable({
        stop: function() {
            var result = $("#select-result").empty();
            $(".ui-selected", this).each(function() {
                var index = $("#selectable li").index(this);
                result.append(" #" + (index + 1));
            });
        }
    });
});

I want var result as value in <input type="hidden" id="select-result" value=""/>

Form -

<form action="XYZServlet" method="get">
 <input type="text" id="from" name="from">
 <input type="hidden" id="select-result" value="">
 <input type="submit" value="Submit"/>
</form>

How can I do this?

Upvotes: 1

Views: 1152

Answers (2)

K K
K K

Reputation: 99

You just need the index, right?

$(function() {
    $("#selectable").selectable({
        stop: function() {
            var result = $("#select-result").empty();
            $(".ui-selected", this).each(function() {
                var index = $("#selectable li").index(this);
                result.append(" #" + (index + 1));
                document.getElementById("my").value = index;
            });      
        }
    });
});

You can view the Value as index(selected item) - <input type="text" id="my" value=""/>

I hope this will work :)

Upvotes: 0

rorfun
rorfun

Reputation: 96

How about:

$(function() {
    $("#selectable").selectable({
        stop: function() {
            var result = $("#select-result").empty();
            $(".ui-selected", this).each(function() {
                var index = $("#selectable li").index(this);
                result.append(" #" + (index + 1));
            });
        $("#select-result").val(result);
        }
    });
});

Upvotes: 1

Related Questions