views:

67

answers:

2
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>
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
It gives an error in Error Console that document.getElementById('stackOverflow').click(); is not a Functionany idea please tell
SAHIL SINGLA
It gives an error in Error Console that document.getElementById('stackOverflow').click(); is not a Functionany idea please tell
SAHIL SINGLA
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
$.ajax({ type:"Post", url:'/Marker/CreateMarkerjson', data:"Color="+ data1 + "
SAHIL SINGLA
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
But the Id on bothside is click
SAHIL SINGLA
How your anchor tag looks like? May you post its declaration here?
Carlos Loth
<a href="/Marker/AllMarkers" id="click">Click to go Back</a>
SAHIL SINGLA
if any problem please tell
SAHIL SINGLA
Is there any mistake?
SAHIL SINGLA
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
I have tried same error no alert comes
SAHIL SINGLA
If no alert is showing on your success function, then your call is failing.
Carlos Loth
insertion in table is doneBut it is saying that$get is not defined
SAHIL SINGLA
and document.getElementById('click') what does it returns?
Carlos Loth
nothingcan u tell mewhat this returnsclick.getElementsByTagName('a');
SAHIL SINGLA
The getElementsByTagName() method returns a NodeList of all a elements with a specified name.
Carlos Loth
click function is not shown by Intelisensewhen I put $get('click').click();
SAHIL SINGLA
May you paste the html source of your rendered page on your question, then I can try troubleshoot it here.
Carlos Loth
Now u can see the Question again
SAHIL SINGLA
$('#click').click()?
Carlos Loth
No error in error consoleBut not working yet
SAHIL SINGLA
What do you mean by console?
Carlos Loth
Error console of Mozilla Firefox
SAHIL SINGLA
Atlast any suggestion
SAHIL SINGLA
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
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
No problem Thanks
SAHIL SINGLA
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
Its not working
SAHIL SINGLA
Error is document.getElementById('clickme').click();is not a functionin error console
SAHIL SINGLA