0

I know only what I need but I do not know how to get that done.

This is the logic of the code, I really hope some of you has the solution.

How can I create in javascript or jQuery a function that will do the following?

If that checkbox is selected, when the button is clicked redirect the user to another page by passing the value of the textarea in the URL.

So that is the logic.

We have three elements.

1)The checkbox

2)The input type button

3) The textarea.

The checkbox is selected, the user clicks on the button and the user goes to another page , and the URL will include the value found in the textarea.

i.e.

http://mydomainname/page.php?ValueThatWasinTextArea=Hello World

Can you help me.

I think it is something simple for a javascript coder.

Thank you so much

4 Answers 4

1
$(function(){
  $(':button').click(function(){
     if($('input[type="checkbox"]').is(":checked")){
        window.location.href = "http://mydomainname/page.php?ValueThatWasinTextArea="+ $('textarea').val();
     }
  });
});

**Of course if there's more than these three elements on the page, you're going to want some more specific selectors

Sign up to request clarification or add additional context in comments.

1 Comment

+1 for providing a solution which works regardless of whether or not the button submits a form.
1

You could subscribe to the submit event of the form and inside test if the checkbox was checked and if yes use window.location.href to redirect to the desired url:

$('#id_of_the_form').submit(function() {
    var value = encodeURIComponent($('#id_of_textarea').val());
    if ($('#id_of_checkbox').is(':checked')) {
        window.location.href = '/page.php?ValueThatWasinTextArea=' + value;
        return false;
    }
});

If the button is not a submit button you can subscribe for the click event of this button and perform the same logic.

Comments

0

Might be some syntax problem because I code this on top of my head

<input id="myCheckbox" type="checkbox" />

<button id="myButton" onClick="buttonClick" />

<input id="myTextArea" type="textarea" />

<script>

   function buttonClick()
   {
     var checkBox = document.getElementById('myCheckbox');
     var textArea = document.getElementById('myTextArea');


     if(checkBox.checked)
     {
        window.location = 'http://mydomainname/page.php?ValueThatWasinTextArea=' + textArea.value;
     }
   }

</script>

Comments

0
$(document).ready(function() {
    $('#btnSubmit').click(function() {
        if($('#chkBox').is(':checked')) {
           window.location = '/page.php?passedValue=' + $('#txtField').val();
        }
    });
};

...

<form>
    <p>
        <input type="checkbox" id="chkBox"> Checkbox</input>
    </p>
    <p>
        <input type="text" id="txtField" value="" />
    </p>
    <p>
        <input type="submit" id="btnSubmit" value="Submit" />
    </p>
</form>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.