1

Why this JavaScript code is not redirecting to the URL ? What is wrong with this code?

function submitform() {
    var hiddenFieldValue = document.getElementById('course').value;
    alert("inindonesia.org/marketplace/tags/"+hiddenFieldValue);
    window.location.href= "inindonesia.org/marketplace/tags/"+hiddenFieldValue); 
    return false;
}

HTML code

<form autocomplete="off" method="POST">
        <p>
            Course Name <label>:</label>
            <input type="text" name="course" id="course" />
            <!--input type="button" value="Get Value" /-->
            <input type="text" name="course_val" id="course_val" />
        </p>
        <input type="submit" value="Submit" onClick="submitform()"/>
</form>
Stacie
  • 235
  • 1
  • 5

4 Answers4

2

As you are relying on button click, what is the need of the form?? As you put that in <form &rt; it is not working. remove form, it will work

        <p>
        Course Name <label>:</label>
        <input type="text" name="course" id="course" />
        <!--input type="button" value="Get Value" /-->
        <input type="text" name="course_val" id="course_val" />
    </p>
    <input type="submit" value="Submit" onClick="submitform()"/>

Remove form.

Navaneeth
  • 2,555
  • 1
  • 18
  • 38
1

You need http:// on the URL. You code updated:

function submitform(){
    var hiddenFieldValue = document.getElementById('course').value;
    alert("inindonesia.org/marketplace/tags/"+hiddenFieldValue);
    window.location.href= "http://inindonesia.org/marketplace/tags/"+hiddenFieldValue); 
    return false;
}
Scott L
  • 549
  • 1
  • 6
  • 13
0

You should use only

window.location = url;

or

window.location.replace = url;

instead off

window.location.href

cause that simulates a link

here is the discussion

How to redirect to another webpage in JavaScript/jQuery?

Community
  • 1
  • 1
Webice
  • 592
  • 4
  • 15
0

you are doing this :

 window.location.href= "inindonesia.org/marketplace/tags/"+hiddenFieldValue);

change it to this :

 window.location.href= "inindonesia.org/marketplace/tags/"+hiddenFieldValue;

NOTE THE ')' that i deleted at the end of the line.

Youness
  • 1,468
  • 1
  • 9
  • 19