1

I have the following code:

function goForit() {
  var passwd;
  passwd = document.forms['giftForm'].pass.value;
  if(passwd=="yes"){
  window.open ('eat.html','_self',false)
  }
  else{
  alert('Password wrong');
  }
}

<form id="giftForm">        
    <input type="text" id="pass" placeholder="" style="font-size:150%;"/>
    <br />
    <input type="button" onClick="goForit()" value="Submit" style="font-size:100%; margin-top:15px;" />                 
 </form>

If someone enters "yes" as the password and clicks the submit button. The user will be redirected to the eat.html page.

I want to pass the .carrot class with the redirect, so the proper image will appear. How do I add the .carrot class to the following code?

need2nobasis
  • 139
  • 2
  • 3
  • 13

2 Answers2

2

Take a look here:

How to redirect on another page and pass parameter in url from table?

Html:

<input type="button" name="theButton" value="Detail" class="btn" data-username="{{result['username']}}" />

then check the redirected url's Location:

Javascript:

$(document).on('click', '.btn', (function() {

    var name = $(this).data('username');

    if (name != undefined || name != null) {
        window.location = '/player_detail?username=' + name;
    }
});​

Also, another StackOverflow Question:

Javascript redirect and Pass Argument to redirected Page

Community
  • 1
  • 1
Hunter Mitchell
  • 7,063
  • 18
  • 69
  • 116
1

You've accepted an answer, but here's an alternative that's closer to the original and doesn't need jQuery:

<button type="button" value="goForit(this)" value="carrot" ... >Go for it</button>

Then in the function:

function gotForit(el) {
  if (el.form.pass.value.toLowerCase() == 'yes') {
    window.location = 'eat.html?username=' + el.value;
  } else {
    alert('oops…');
  }
}
RobG
  • 142,382
  • 31
  • 172
  • 209