views:

949

answers:

2

Hello:

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

A: 

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);
    });
});
Mark Hurd
I love jQuery. It's a time saver. However, the OP made no mention of it.
Jose Basilio
Thanks - I haven't really researched jQuery so I am kind of hesitant to use this right now. I wanted to get a better grasp of Javascript, before using newer more efficient methods. Nonetheless, thank you for your reply.
+1  A: 

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>
Jose Basilio
Thanks - This is working well!