1

I need your help. This should be a simple-to-answer question. I want to modify a site to my needs and this is how far I got.

I have a formular


<form>
  <div><select id="select1"><option>value x</option></select></div>
  <div><select id="select2"><option>value x</option></select></div>
  <div><input type="submit" name="submit" value="Submit changes"></div>
</form>

I want to check whether the value of the select-elements is equal or not. This should happen when I click the submit-button. If they are equal there should be a popup with text like: Are you sure this is right?

So I call the value of the selectfields with


function show_alert() {
  var x = document.getElementById('select1').value;
  var y = document.getElementById('select2').value;

  //If the values are equal there should be an alert
  if (x=y) {
    alert ('Are you sure this is right?');
  }
}

I guess that I should add an onClick-handler to the submit button like

  <div><input type="submit" name="submit" value="Submit" onclick='show_alert()'></div>
How do I add this handler when I have no access to the site? If there is anything wrong with my idea I'd be happy to hear your comment.

Thanks.

7
  • 1
    First - What do you mean by 'when I have no access to the site'? Second - onclick='show_alert' Third - if (x==y) instead of if (x=y) Commented Feb 14, 2011 at 13:42
  • What do you mean by you have no access to the site? If you have no access, how can you add JavaScript code? Commented Feb 14, 2011 at 13:42
  • What is your intent? Think about it - if everyone could edit any page on the web :). Try saving (Ctrl+S) the page on your computer and edit the code then. Commented Feb 14, 2011 at 13:43
  • I assume he means that he can't change the HTML. Commented Feb 14, 2011 at 13:44
  • If you have no access to the site, then you can't modify it at all. You could run a script like this in Firebug, but you can't change site files without access. Also, your if statement needs to be "if(x===y)" (comparison instead of assignment operator). Commented Feb 14, 2011 at 13:44

3 Answers 3

1

You could do something like this:

markup:

<form id="myform" onsubmit="show_alert(this);return false;">
    <div><select id="select1"><option>value x</option></select></div>
    <div><select id="select2"><option>value x</option></select></div>
    <div><input type="submit" name="submit" value="Submit" /></div>
</form>

js:

function show_alert() {
  var x = document.getElementById('select1').value;
  var y = document.getElementById('select2').value;
  if(x == y && confirm('Are you sure this is right?')){
      document.myform.submit();
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can register an event handler using Javascript.

Note that you need to change your if statement to if (x === y).
Writing if (x=y) will assign x to y, then pass the value of y to the if check.

Comments

0

Note: you have an error in script. Is if (x==y) not x=y.

If I understood the question right you want to modify a file with no access to it?...

You don't need access to the site you need access to the file with the form. FTP access is most common.

If you don't have... you can't.

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.