-1

I'm quite new to jQuery and html, looked through google and these forums to no avail.

This is my HTML Code

<html>
<head>
    <link rel="stylesheet" href="reset.css" type="text/css">
    <link rel="stylesheet" href="index.css" type="text/css">
    <script src="./jquery.js" type="text/javascript"></script>
</head>
<body>
    <div id="container">
        <form id="form">
            <h1>Koks yra leistingas greitis miesto ribose?</h1>
            <input type="radio", id="ats1", name="atsakymas", value="1" ></input>
            <label for="ats1"> a</label>
            <br>
            <input type="radio", id="ats2", name="atsakymas", value="2" ></input>
            <label for="ats2"> b</label>
            <br>
            <input type="radio", id="ats3", name="atsakymas", value="3" ></input>
            <label for="ats3"> c</label>
            <br>
            <button id="Rinktis" type="submit">Rinktis</button>
            <button id="close">Išeiti</button>
        </form>
    </div>
    <script src="./index.js" type="text/javascript"></script>
</body>

And this is my jQuery line that I think should redirect me to another page

    $("#Rinktis").click(function (){
        document.location.href = "http://google.com";
        return;
    })

but it is doing nothing, am I doing something wrong?

Kevinas
  • 1
  • 4
  • Use `window.location.href` instead of `document` – Zak Dec 30 '21 at 00:12
  • @Zak In modern browsers `document.location` is already mapped to `window.location` (although the latter is preferrable). – Roko C. Buljan Dec 30 '21 at 00:50
  • Use the [browser console (dev tools)](//webmasters.stackexchange.com/q/8525) (hit `F12`) and read any errors. The dev tools provide a **Network** tab. Look for both script resources. See what happens when you click the button. Please confirm: Is the resource _found_ (e.g. HTTP 200 response)? If not, which _actual URL_ is requested? Amend the URL accordingly. Try `https` instead of `http`. If you get _“`ReferenceError`: `$` is not defined.”_, make sure `jquery.js` actually exists and includes jQuery. Did the answer below work? Remove all closing `` tags; these are redundant. – Sebastian Simon Dec 30 '21 at 01:12
  • @Zak it doesn't work no matter wether it's document or window. Sebastian Simon yes I have checked through the console and there are no errors or warnings, the jQuery.js script is loading fine – Kevinas Dec 30 '21 at 10:16
  • `$("#form").submit(function(event){ event.preventDefault(); window.location.href = "http://google.com"; });` – Zak Dec 30 '21 at 16:10

1 Answers1

-1

Your code (in JS) should be:

$("#Rinktis").click(function (e) {
    e.preventDefault();
    window.location.href = "http://www.google.com";
})

The return statement there is unnecesary. The e parameter in the click function represents the Event object. Since you're overriding the form's submit action (not sure why), you need that first line to stop the default action.

Alex_89
  • 371
  • 5
  • 15