Simulate click of javascript Button - javascript

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

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);">

$("#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);">

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

Related

clicking a button in javascript without button id or class

I'm trying to use AppleScript to click a button on a webpage, however the button I need to click doesn't have an ID or Class, and it's located inside a div/input tag:
<div style="margin:0;padding:0;position:absolute;width:100px;right:20px"><input style="padding:4px;height:2em;width:100px" type="submit" value="save" tabindex="-1"></div>
<input style="padding:4px;height:2em;width:100px" type="submit" value="save" tabindex="-1">
I tried using querySelector, but it didn't work. How do I go about clicking this button?
In this case an attribute selector would work fine, I'd imagine.
document.querySelector('input[value="save"]')
More information from MDN: https://developer.mozilla.org/en-US/docs/Web/CSS/Attribute_selectors

How to change display value of the Submit button on page load using jquery

I am new to JQuery and need suggestions on following requirement.
I have a form with a submit button as below. Page accepts locale as an input parameter. Depending on the value of locale, on page load I am populating the labels of the input fields in respective language using jQuery.i18n.properties.js plug-in, but I could not update the display value of the button.
Please suggest solution or if there is another way to achieve this.
HTML code:
<input type="submit" data-inline="true" id="submit" value="Submit"/>
Have tried below jQuery options to update the button label:
$("#submit").val($.i18n.prop('submit'));
$("#submit").html($.i18n.prop('submit'));
$("#submit").prop('value',($.i18n.prop('submit')));
$("#submit").text($.i18n.prop('submit'));
None of them worked. But I see the value gets updated as below in Developer tools window, for this button.
<div class="ui-btn ui-input-btn ui-corner-all ui-shadow">
Submit
<input type="submit" id="submit" value="New Text">
</div>
Try $("#submit")[0].value = $.i18n.prop('submit');. Does that work for you?
(Even though it's a JS workaround, not a JQuery solution)
If your button is an input tag, use the jQuery val:
function changeBtnText() {
$("#submit").val("My new button text");
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="submit" id="submit" value="My button">
<button type="button" onclick="changeBtnText()">Change button text</button>
If your button is a button tag, use the jQuery text (or html):
function changeBtnText() {
$("#submit").text("My new button text");
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button type="submit" id="submit">My button</button>
<button type="button" onclick="changeBtnText()">Change button text</button>
Note: I recommend giving your button an ID different from "submit" to avoid confusion.

Form resetting is not working

I'm using the following code to reset the form fields.
document.getElementById("form1").reset();//form1 is the form id.
It is not working. I also tried with JQuery. Even JQuery also not working.
$("#reset").click(function(){
$('form1')[0].reset();
});
My html code is
<form name="form1" id="form1" method="post">
<h3>Personal Information</h3>
<h4>Name</h4>
<input type="text" id="fname" name="fname" maxlength=50 size=11/>
<input type="text" id="mname" name="mname" maxlength=15 size=8/>
<input type="text" id="lname" name="lname" maxlength=50 size=11/>
<input type="button" id="reset" value="Reset" onclick="Reset()"/>
</form>
I'm following W3Schools. Here is my Fiddle. Can you please explain the mistake?
The problem here is that you've set the id of your button to "reset". This automatically overwrites the built-in reset method of the form element.
The solution is to use a different id attribute for your button.
So instead of:
<input type="button" id="reset" value="Reset" />
Use something like:
<input type="button" id="reset-button" value="Reset" />
See this fiddle.
Have you simply try this : Reset
<input type="reset" value="Reset"/>
I finally solved my issue. the problem is "id=reset". Because it overrides the reset method . I changed it to id="reset1". Now it is working
If your objective is only to reset the form, you could try this:
<input type="reset" id="reset" value="Reset" onclick="this.form.reset();"/>
Looks like your seleting $('form1') as in an element with a tag name of form1, while i think you actually want to select it by the id:
$('#form1')[0].reset();
With jQuery, the correct selector is :
$('#form1') // Select with ID
OR
$('form[name=form1]') // Select with name
I've updated your fiddle.
Why vanilla js isn't working:
You don't have...
document.getElementById("form1").reset();//form1 is the form id.
...within a reset function. You could do this:
function Reset() {
document.getElementById("form1").reset();//form1 is the form id.
}
Why jQuery isn't working:
You don't need to do all that you're doing. It's much more simple than that. Also, look at your 'form1' selector. You should likely add '#form1' instead. jQuery selects are different than the getElementByID function. As you can probably assume by the name, the getElementByID function is already getting the element by the ID; however with jQuery you have to specify those things. Also, don't really need the onClick attribute with jquery.
Ok, see my new jsfiddle:
http://jsfiddle.net/ty9rU/17/
So i renamed the button to reset_btn
Basicly you had an element called reset inside the form, and that caused the issue.

Javascript to click button

Below i have a html code
<td valign="top" id="UserManagebutton1"><input type="button" value="Add" class="inputbutton">
<input type="button" value="Delete" class="inputbutton">
<input type="button" value="Import" class="inputbutton">
<input type="button" value="Export" class="inputbutton">
<input type="button" value="Change Status" class="inputbutton">
<input type="button" value="Purge AD Users" class="inputbutton"></td>
I tried putting this into my console but it did not work. I am trying to click the Add Button.
Below is the code i used.
document.getElementById('Add').click();
Any idea why its not working? It works with others.
Since it seems that you do not have control over the page you are trying to manipulate, you will have to work with the HTML as it is. In this case, providing you do have control over the browser you are using (are you doing this from the console?), you can use document.querySelector: Documentation here.
So, in your specific case, you'd want to do
document.querySelector('[value="Add"]').click()
Or, to make things more specific, you could do
document.querySelector('#UserManagebutton1 input[value="Add"]').click()
Here is a fiddle with an example: http://jsfiddle.net/xonev/V6SQ2/
Use
<input type="button" id="Add" value="Add" class="inputbutton">
document.getElementById('Add').click();
Note you need id="Add" in order to get it using getElementById('Add')
You have no unique identifiers for your buttons. Make an ID that is the same as the value.
<td valign="top" id="UserManagebutton1">
<input type="button" id="Add" value="Add" class="inputbutton">
<input type="button" id="Delete" value="Delete" class="inputbutton">
<input type="button" id="Import" value="Import" class="inputbutton">
<input type="button" id="Export" value="Export" class="inputbutton">
<input type="button" id="Change" value="Change Status" class="inputbutton">
<input type="button" id="Purge" value="Purge AD Users" class="inputbutton">
</td>
Use this, then your code will work.
In your code you have set the value of the button to Add which simply means that the text that will appear on the button is Add. Value, as an attribute sets the text of that element.
If you want to access that element by id, you need to specify an id attribute for it, like this:
<input id="Add" class="inputbutton" type="button" value="Add">
And to access the button you could simply use the code that you have used in your post:
document.getElementById('Add').click();
Also, if you do not want to modify your current code, you could simply use another javascript selector to get that specific element, such as:
document.getElementsByClassName('inputbutton')[0].click();
Moreover, if you want to add click event listeners for the other elements, as you do for the Add button, you could do some Event Delegation which is great to manage performance:
// Bind the event listener to the top most element that contains your event targets
document.addEventListener('click', function(event){
// Get target of the event
var target = event.target || event.srcElement;
switch(target.value) {
case('Add'):
alert('Add button clicked!');
break;
...
// Add cases for the other buttons as well
}
});
Here is a simpler version that only listens for clicks on the Add button:
document.addEventListener('click', function(event){
var target = event.target || event.srcElement;
if (target.value == 'Add') {
alert('You clicked?');
}
});
If you are not using any JS libraries, try:
document.getElementsByName('Add')[0].click()
UPDATED - correction below
Since the buttons don't have name attributes (thanks #Rocket Hazmat) and since you cannot change the HTML, you can try this instead:
document.getElementsByTagName('input')[0].click();
// [0] for add button,
// [1] for Delete button,
// etc.

how to add fancybox when an input submit button is clicked?

apparently it seems like this fancybox only works for anchor tags that has an ID or class ?,
but i want to use it in a submit button...is there a way to use it in that element ?
e.g this doesn't work because fancybox needs an href that will pull the contents
<input type="submit" id="submitme" name="submitme" value="SUBMIT ME" />
fancy box code
$("#submitme").fancybox();
Refer it by #,
$("#submitme").fancybox();
The issue is not whether fancybox can be bound to a submit button or not. The actual issue is that your submit button doesn't tell fancybox the target content to open and the type of content it is.
So having this :
<input type="submit" id="submitme" name="submitme" value="SUBMIT ME" />
... will work if you hard code the missing elements href and type in your costom script like :
$("#submitme").fancybox({
type : "iframe",
href : "http://jsfiddle.net"
});
See JSFIDDLE
Optionally, you can hard code any html content too like :
$("#submitme").fancybox({
content : "<p>Any html as content</p>"
});
See JSFIDDLE
You are missing the # in your node reference. But that's probably just because you typed out your code in the question. You could always style a hyperlink to look like a button, give it a URL and attach the fancybox to it:
<!--<input type="submit" id="submitme" name="submitme" value="SUBMIT ME" />-->
<a href="somewhere.htm" id="submitme" name="submitme" value="SUBMIT ME" >SUBMIT ME</a>
$("#submitme").click(function(event){
event.preventDefault();//stop the hyperlink from navigating away from the current page
}).fancybox();

Categories