Can we Made an anchor tag autoclick in the success function of Ajax Script?
Does it Possible we Click an anchor tag through Ajax Script?
if Yes then how?I am using Ajax in asp.net MVC?
This is the Viewsource of Partial View
<script language="javascript" type="text/javascript">
$(document).ready(function(){
alert("Button clicked");
$("#bt1").click(function(){
var data2 = $('#txt2').val();
var data1 = $('#Color').val();
$.ajax({
type:"Post",
url:'/Marker/CreateMarkerjson',
data:"Color="+ data1 + "&txt2=" + data2,
success:function(result)
{
alert(result);
$get('click').click();
},
error:function(result)
{
alert("fail");
}
});
});
});
</script>
<script type="text/javascript">
$(document).ready(function() {
$("#datepicker").datepicker();
});
</script>
<form action="/Marker/CreateMarkerPartial" method="post">
<fieldset>
<legend>Fields</legend>
<p>
<label for="Id" id="ID">
Id:</label>
<input type="text" id="txt1" />
</p>
<p>
<label for="CompanyName">
CompanyName:</label>
<input type="text" id="txt2" />
</p>
<p>
<label for="Color">
Color:</label>
<input id="Color" name="Color" type="text" value="" />
</p>
<p>
<input type="button" id="bt1" value="create" />
</p>
<div id="datepicker"></div>
</fieldset>
</form>
<div>
<a href="/Marker/Index">Back to List</a>
<a href="/Marker/AllMarkers" id="click">click</a>
</div>
views:
67answers:
2
A:
Update answer. Merged the two $(document).ready functions and changed the $get('click') to $('#click'). Let's give it a try.
<script language="javascript" type="text/javascript">
$(document).ready(function () {
alert("Button clicked");
$("#bt1").click(function () {
var data2 = $('#txt2').val();
var data1 = $('#Color').val();
$.ajax({
type: "Post",
url: '/Marker/CreateMarkerjson',
data: "Color=" + data1 + "&txt2=" + data2,
success: function (result) {
alert(result);
$('#click').click();
},
error: function (result) {
alert("fail");
}
});
});
$("#datepicker").datepicker();
});
</script>
<form action="/Marker/CreateMarkerPartial" method="post">
<fieldset>
<legend>Fields</legend>
<p>
<label for="Id" id="ID">
Id:</label>
<input type="text" id="txt1" />
</p>
<p>
<label for="CompanyName">
CompanyName:</label>
<input type="text" id="txt2" />
</p>
<p>
<label for="Color">
Color:</label>
<input id="Color" name="Color" type="text" value="" />
</p>
<p>
<input type="button" id="bt1" value="create" />
</p>
<div id="datepicker">
</div>
</fieldset>
</form>
<div>
<a href="/Marker/Index">Back to List</a> <a href="/Marker/AllMarkers" id="click">click</a>
</div>
Carlos Loth
2010-05-21 05:41:30
It gives an error in Error Console that document.getElementById('stackOverflow').click(); is not a Functionany idea please tell
SAHIL SINGLA
2010-05-21 06:15:52
It gives an error in Error Console that document.getElementById('stackOverflow').click(); is not a Functionany idea please tell
SAHIL SINGLA
2010-05-21 06:16:33
Oh! It it seems you are trying to accomplish is to run this statement on the server side, isn't it? I was thinking you were trying to run it on the client side. May you provide an example of your code?
Carlos Loth
2010-05-21 06:19:10
$.ajax({ type:"Post", url:'/Marker/CreateMarkerjson', data:"Color="+ data1 + "
SAHIL SINGLA
2010-05-21 06:22:34
First of all I think you need to provide the correct Id attribute value on your anchor tag. If you are using IE8 you can using the Developer Tools window (press F12 in your browser) to inpect the elements of your page and discover the id of your anchor. The parameter you must pass to document.getElementById or to $get is the value you set on your anchor tag id attribute. In my example, it was "stackOverflow" but your code it is another value that you, or the runtime have defined.
Carlos Loth
2010-05-21 06:28:49
But the Id on bothside is click
SAHIL SINGLA
2010-05-21 06:30:59
How your anchor tag looks like? May you post its declaration here?
Carlos Loth
2010-05-21 06:31:40
<a href="/Marker/AllMarkers" id="click">Click to go Back</a>
SAHIL SINGLA
2010-05-21 06:34:16
if any problem please tell
SAHIL SINGLA
2010-05-21 06:34:36
Is there any mistake?
SAHIL SINGLA
2010-05-21 06:39:28
It is becoming more difficult to figure out what is going on there, than I initially thought. I still think the problem is with your id. So, is the "alert(result)" showing up? If yes, try to check what the following statement shows "alert(document.getElementById('click'))". If it undefined, the problem is the Id. Another thing is that you can have more than one html tag declared with id attribute="click".
Carlos Loth
2010-05-21 06:43:05
I have tried same error no alert comes
SAHIL SINGLA
2010-05-21 06:51:09
If no alert is showing on your success function, then your call is failing.
Carlos Loth
2010-05-21 07:03:20
insertion in table is doneBut it is saying that$get is not defined
SAHIL SINGLA
2010-05-21 07:07:09
and document.getElementById('click') what does it returns?
Carlos Loth
2010-05-21 07:18:49
nothingcan u tell mewhat this returnsclick.getElementsByTagName('a');
SAHIL SINGLA
2010-05-21 07:20:31
The getElementsByTagName() method returns a NodeList of all a elements with a specified name.
Carlos Loth
2010-05-21 07:25:41
click function is not shown by Intelisensewhen I put $get('click').click();
SAHIL SINGLA
2010-05-21 07:26:31
May you paste the html source of your rendered page on your question, then I can try troubleshoot it here.
Carlos Loth
2010-05-21 07:26:35
Now u can see the Question again
SAHIL SINGLA
2010-05-21 07:38:40
$('#click').click()?
Carlos Loth
2010-05-21 07:52:02
No error in error consoleBut not working yet
SAHIL SINGLA
2010-05-21 07:55:37
What do you mean by console?
Carlos Loth
2010-05-21 07:58:35
Error console of Mozilla Firefox
SAHIL SINGLA
2010-05-21 08:07:26
Atlast any suggestion
SAHIL SINGLA
2010-05-21 08:32:59
I've updated the answer, if it doesn't work, I don't know what else we can do. I'd also try to rename the id of my anchor or change the javascript to change from calling the click event of the anchor to window.location.assign('/Marker/Index').
Carlos Loth
2010-05-21 08:45:02
I need to go now, it is 5:45 AM here. I go to work at 6:30. I'm sorry for not being able to help you more. Good luck!
Carlos Loth
2010-05-21 08:46:34
No problem Thanks
SAHIL SINGLA
2010-05-21 08:53:01
A:
you can fire the click handler of the anchor tag that you want clicked.
for example:
<a id="clickme" href="somelink.html">Click me</a>
now there must be an event being fired from your ajax script on the successful completion of the request. In that function do
document.getElementById('clickme').click();
ovais.tariq
2010-05-21 05:43:09
Error is document.getElementById('clickme').click();is not a functionin error console
SAHIL SINGLA
2010-05-21 06:41:16