subbu royal subbu royal - 7 months ago 18
Javascript Question

Form action value not getting from attr() function

I have one form in html

html file

<blc:form value method=POST action="@{/cart/add}">
.....
.....
<div>
<input class="btn btn-success" type="button" value="Add to Cart" id="addToCart"/> <!-- submit button -->
</div>
<div id="productInCart"></div>
......
</div>
</blc:form>


when I click the button I need to send
ajax
request to the server by taking request from
form action attribute


JavaScript

$document.on('click','#addToCart',function(e){
alert('test1')
var url = button.parents('form').attr('action')
alert("request url:-"+url)//control is not reaching up to here
$.ajax({
type: "POST",
url: url,
success: function(){
$("#productInCart").html("")
alert('success')
}
error: function(){
alert('fail')
}
});
});


Control is entering into function and printing the
alert
value
test1
but unable to reach after
url
variable, above code is failing to submit the request to the server through
ajax
call because it is failing to get the value of
action
attribute from the form.
Can anyone help me what is wrong in above code.

Answer
$("[name=form]").attr("action",'url');
       or
$(this).parents("form").attr('action')
Comments