Passing List Text to Javascript Popup URL

Is it possible to pass the contents of a text box or list to a javascript code url window.open(URL)? I have an asp.net list control that displays URL values. When an end user clicks on another list, a specific URL is specified in the list of URLs. I am trying to pass this URL to the above javascript code, but I don't know the correct syntax for this. This code will execute as an onclick event.

For clarification, it looks like entering "+ ListBox.Text.ToString() +"or β€˜" & List.Text & "’"to add the contents of a list to something else, such as a text field. Is there a specific syntax to do the same, but add listbox.text to javascript?

Thanks,

Dfm

+3
source share
2 answers

Just add the client side onclick handler to your list, 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>
+3
source

Of course, this should be pretty easy with jQuery. Obviously, generating URLs can be reduced to one statement, but should give you a general idea.

$(document).ready(function() {
    $("your-element").click(function() {
        var str = $("#listbox-id").val();
        var url = "your-url.com/" + str;
        window.open(url);
    });
});
0
source

Source: https://habr.com/ru/post/1710806/


All Articles