Question

Is it possible to pass the contents of a textbox or Listbox into the URL portion of the javascript code window.open(URL)? I have an asp.net listbox control that displays URL values. When ever an end user clicks on another listbox, the URL listbox provides the specific URL. I am trying to pass this URL into the above javascript code, but I do not know the correct syntax to do this. This code will execute as an onclick event.

For clarification, similar to typing “+ ListBox.Text.ToString() +” or ‘” & List.Text & “’” to add the content of a listbox into something else, such as a textbox. Is there specific syntax to do the same, but add the listbox.text into javascript?

Thank you,

DFM

Was it helpful?

Solution

Simply add a client-side onclick handler to your listbox as shown below:

<asp:ListBox id="ListBox1" runat="server" .....
           onclick="openPopup(this)">
        ........
</asp:ListBox>

Then add the following javascript code:

<script type="text/javascript">
    function openPopup(e){
      window.open(e.value); 
    }
</script>

OTHER TIPS

Sure, this should be pretty easy with jQuery. Obviously the URL generation could be reduced to a single statement, but should give you the general idea.

$(document).ready(function() {
    $("your-element").click(function() {
        var str = $("#listbox-id").val();
        var url = "your-url.com/" + str;
        window.open(url);
    });
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top