Question

In a input form in a HTML file, the user is supposed to put an URL (let's call it thislink). Then I want, when the user clicks on the submit button, to open a new window whose URL integrates thislink, that is its URL should be: '/selection/yes/?value='+thislink'.

Here are 2 tentatives of code:

1st tentative:

<form id="urlarticle">
  <input type='text' name='thislink'>
  <input type='submit' value='Select' onclick = function() {window.open('/selection/yes/?value='+thislink)};>
</form>

2nd tentative:

<form id="urlarticle">
  <input type='text' name='thislink'>
  <input type='submit' value='Select'>
</form>

<script type='application/javascript'>
    $("#urlarticle").submit(function() {
        window.open('/selection/yes/?value='+thislink);
    });     
</script> 

But both tentatives are not working, any help to get the right way to write it appreciated!

Was it helpful?

Solution

Since you tagged jQuery, you could do it like this:

http://jsfiddle.net/729uh/

Javascript:

$("#urlarticle").submit(function() {
    var linkValue = $('input[name="thislink"]').val();
    window.open('/selection/yes/?value='+linkValue);
}); 

Html:

<form id="urlarticle">
    <input type='text' name='thislink'/>
    <input type='submit' value='Select'/>
</form>

What this does is:

  • use jQuery to select the input that has a name attribute with value thislink
  • take the value that was written in it
  • append that value to your link
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top