2

How can i simulate a click of the button below? I tried using the javascript $("#Save").click() but it didnt work. Does this have to do with it not working because there is no id but name?

<input class="button" type="submit" name="Save" value="Save" onclick="OnSubmit(this.form);">

What javascript command in my browser would i use to simulate the click of Save following something like i tried to use?

Much help appreciated! Im new to this

1
  • 1
    You need the id="Save" because the # sign in your JQuery selector means it is selecting the ID following it (Save in your case). Commented Sep 6, 2013 at 15:08

3 Answers 3

8

It appears your using jQuery with an id selector (# denotes an id), however the element doesn't have an id. Since the element does have a name attribute, an attribute selector can be used by jQuery. An appropriate selector would be:

$('input[name="Save"]').click(); //Assuming no other elements have name=Save

JS Fiddle: http://jsfiddle.net/pJD3R/

You could also change the markup to work with your existing selector by adding an id attribute:

<input id="Save" class="button" type="submit" name="Save" value="Save" onclick="OnSubmit(this.form);">
Sign up to request clarification or add additional context in comments.

2 Comments

Supousing he has not another input with name 'Save'
@Ander2 Agreed another element with the name Save would cause issues.
4

$("#Save").click() mean you target an element with the id save but you don't have any id on your input.

<input class="button" id="save" type="submit" name="Save" value="Save" onclick="OnSubmit(this.form);">

Comments

1
$('input[name=Save]').click(function(){
    alert('Do Something');
});

3 Comments

I thought the value required quotes, but I just tested a fiddle that worked without the quotes. Interesting. Documentation states: value: An attribute value. Can be either an unquoted single word or a quoted string. I didn't know that.
@KevinBowersox Yes in this case works, but is better use always quotes, thanks for pointing that.
So something like my name would fail due to the space?

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.