1

I want to add "https://" to a form on submission, without it appearing the text submission box. So far I have a script:

<script>
    function formSubmit(){
        var x=document.getElementById(“formname”);
        for (var i=0;i<x.length;i++){
            if(x.elements.item(i).id == 'q' && x.elements.item(i).value != "" ) 
                x.elements.q.value='https://'+x.elements.item(i).value;
            document.getElementById("formname").submit();
        }
</script>

and the form:

<form name="formname" method="GET" action="http://google.com/search" onsubmit="return (this.q.value == '') ? false : true; >
    <input type="text" name="q" size="32" maxlength="256" value="" />
    <input type="submit" name="btnG" value="Search" />

but can't figure it out. anybody see what I'm doing wrong?

1
  • may need to change document.getElementById(“formname”); to document.getElementByName(“formname”); Commented Feb 10, 2015 at 20:35

1 Answer 1

1

You definitely won't want to try submitting the form again in your function; after it finishes calling, it'll continue to submit as long as you don't return false.

Your function should just be:

function formSubmit(){
    var e=document.forms.formname.q;
    if(e.value){
        e.value='https://'+e.value;
    } return true;
}

And your form's onsubmit attribute...

<form onsubmit="return this.q.value != '' && formSubmit()">
Sign up to request clarification or add additional context in comments.

2 Comments

hmm, nothing is happening when I click Search. should it be this.e.value? When I change it to that, it submits, but doesn't append...
My bad, I made a mistake with the form's onsubmit part. See the updated answer.

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.