I have this action link in view:
@Html.ActionLink("Edit", "Edit", null, new {@id="editLink", @class="button"})
I would like to call a java script method to make the URI at runtime from my Selected List Box Item.
@Html.ListBox("EmployeeName", (IEnumerable<SelectListItem>)ViewBag.selectlist, new { @id = "saglistbox", @class = "SAGListbox" })
As a first step, when i am trying to just display an alert using JavaScript:
<script type="text/javascript">
$('#editLink').click(function () {
alert("hello");
});
</script>
For some reason this is not working as expected.
I have this action link in view:
@Html.ActionLink("Edit", "Edit", null, new {@id="editLink", @class="button"})
I would like to call a java script method to make the URI at runtime from my Selected List Box Item.
@Html.ListBox("EmployeeName", (IEnumerable<SelectListItem>)ViewBag.selectlist, new { @id = "saglistbox", @class = "SAGListbox" })
As a first step, when i am trying to just display an alert using JavaScript:
<script type="text/javascript">
$('#editLink').click(function () {
alert("hello");
});
</script>
For some reason this is not working as expected.
Share Improve this question edited Jul 15, 2013 at 13:37 anar khalilov 17.5k9 gold badges51 silver badges63 bronze badges asked Jul 15, 2013 at 13:25 SrinivasSrinivas 2,53910 gold badges48 silver badges70 bronze badges3 Answers
Reset to default 5You were not binding event at when DOM is pletely loaded.
<script>
$(function () {
$('#editLink').click(function () {
alert("hello");
//To get selected value use
var selected = $("#EmployeeName").find(':selected').text();
//To stop default behaviour
return false;
});
});
</script>
Second make the URI at runtime from my Selected List Box Item
then you have to stop it default behavior thus used return false
Try this:
<script type="text/javascript">
$(document).ready(function()
{
$('#editLink').click(function ()
{
alert("hello");
});
});
</script>
You can pass any html attribute as parameter to generate the action link. You can do this as well:
@Html.ActionLink("Edit", "Edit", null, new { @id="editLink", @class="button", onclick = "myfunction()" })