i have implemented a java script to bounce an image on my asp.net mvc 3 web application, if i write the Jscript as follow it will work fine:-
$(document).ready(function () {
$("#to-get-bigger").mouseover(function () {
$(this).effect("bounce);
});
});
but if i write it this way it will not work !! ,, so what might be the reason behind this :-
$(function () {
$("to-get-bigger").mouseover(function () {
$(this).effect("bounce");
});
});
You are missing the hash # for your selector. I've fixed it:
$(function () {
$("#to-get-bigger").mouseover(function () {
$(this).effect("bounce");
});
});
You forgot the # in front of "to-get-bigger".
It's a typo.
Related
Can someone explain me why this code below does not hit the on change function?
HTML
<div class="form-group">
#Html.DropDownList("Relatorios", ViewBag.Tipos as SelectList, new { id = "Relatorios"})
</div>
Script
$(document).ready(function () {
$("#Relatorios").on("change", function () {
alert("Success");
});
});
I expected that function to hit when I change de option in the drop down list
I resolved the problem with the code below
$(window).load(function () {
$("#Relatorios").on("change", function () {
settabledata();
});
I am trying to append a script tag with a script on the head of a page, but I am appending the code and it doesnt work. Probably because the page is already running.
How can I have something like this work?
$(document).ready(function () {
$('a').on('click', function () {
$('head').append('<script>console.log("running")</script>');
});
});
http://jsfiddle.net/jtjmy5gy/6/
Try
DEMO
$(document).ready(function () {
$('a').on('click', function () {
$('<script>').text('console.log("running")').appendTo('head');
});
});
EDIT
call Function using above Example : DEMO
$(document).ready(function () {
$('a').on('click', function () {
$('<script>').text('test()').appendTo('head');
});
});
function test(){
console.log("running")
}
Edit . you can also append script tag like below : DEMO
$("head").append($("<script />", {
text: 'test()',
}))
$("head").append($("<script />", {
html: 'test()',
}))
$("head").append($("<script />", {
src: url,
}))
You can put the script into a separate file, then use $.getScript to load and run it.
$.getScript("test.js", function(){
alert("Running test.js");
});
Source: Rocket Hazmat
How to dynamically insert a <script> tag via jQuery after page load?
I need help with my UI element. I want to show next calendar when first close.
Unfortunately function onClose doesnt run :/ I don't know why..
$(function () {
$('#datepickerDate1').datepicker({
onClose: function () {
$("#datepickerDate2").datepicker("show");
}
});
});
Website online:
http://nauka.cf/issue/ui-elements/search-box-1/index.html
Thanks!
solve:
$(function () {
$('#datepickerDate1').datepicker().on('changeDate', function (ev) {
$('#datepickerDate1').datepicker('hide');
$('#datepickerDate2').datepicker('show');
});
});
Try following will solved your issue.
$("#datepickerDate2").focus();
i am developing an Asp.net application when i call javascript function from codebehind i found that for example:
click event not fired
i have a javascript function that fill the dropdown list with items using ajax but when page loaded i found dropdown list empty
i am using RegisterClientScriptBlock to execute the javascript code
so is there any solution for these problems?
code snippet:
code behind:
ClientScript.RegisterClientScriptBlock(this.GetType(), "ClientBlock", javacode.ToString());
this what in javacode variable :
<script type="text/javascript">
<!--
function ExecuteScript()
{
$("#divGender input").click();
GetDMspecifyList(5);
$("cp1_drpDMSpecify").removeAttr('disabled');
$("cp1_drpDMSpecify option:selected").val(4);
$("#divFamily input").click();
}
</script>
// -->
this the function used to fill dropdown list but it is not working
function GetDMspecifyList(DMID) {
$("#DMLoader").show();
$.getJSON('FillDropDownLists.aspx?DMTypeID=' + DMID, function (types) {
$.each(types, function () {
$("#cp1_drpDMSpecify").append($("<option></option>").val(this['DMTypeCode']).html(this['DMTypeName']));
});
$("#DMLoader").hide();
$("#DMSpecify_span").show();
$("#cp1_hdDMType").val($("#cp1_drpDMSpecify").val());
$("#cp1_drpDMSpecify").removeAttr('disabled');
});
}
First of all the function "ExecuteScript()" is lacking it's closing curly brace "}".
Also, is the ExecuteScript() function called anywhere?
EDIT
You could try something similar to the code below :
<script type="text/javascript">
<!--
function ExecuteScript() {
$("#divGender input").click();
GetDMspecifyList(5, function() {
$("cp1_drpDMSpecify").removeAttr('disabled');
$("cp1_drpDMSpecify option:selected").val(4);
$("#divFamily input").click();
});
}
function GetDMspecifyList(DMID, callback) {
$("#DMLoader").show();
$.getJSON('FillDropDownLists.aspx?DMTypeID=' + DMID, function (types) {
$.each(types, function () {
$("#cp1_drpDMSpecify").append($("<option></option>").val(this['DMTypeCode']).html(this['DMTypeName']));
});
$("#DMLoader").hide();
$("#DMSpecify_span").show();
$("#cp1_hdDMType").val($("#cp1_drpDMSpecify").val());
$("#cp1_drpDMSpecify").removeAttr('disabled');
callback();
});
}
$(function() { ExecuteScript(); });
// -->
</script>
I have a javascript file that Select a button using jQuery and check it for validation.the code is:
$(document).ready(function () {
$('#<%= btn_Insert.ClientID %>').on("click", function (event) {
if (Validate() != true) {
event.preventDefault();
}
});
function Validate() {
return (1 == 1);
});
but I can' get ClientID for button and I get this Error:
Object doesn't support this property or method
where is my mistake?How I can get Client ID for a server side Button?
thanks
EDIT 1)
When I add script in head part of page it works but when I write it to a JS file and add reference to it in head part it does not work
I suspect that the error comes from the fact that event has a special meaning in some browsers. Try renaming the variable:
function Validate() {
return (1 === 1);
}
$(function () {
$('#<%= btn_Insert.ClientID %>').on('click', function (evt) {
if (!Validate()) {
evt.preventDefault();
}
});
});
or even easier :
$('#<%= btn_Insert.ClientID %>').on('click', function() {
return Validate();
});
UPDATE:
Now that you have edited your question it is clear where the problem is. You are trying to use server side tags (<%= %>) in a separate javascript file. That's impossible. They will render literally and will not be processed by ASP.NET.
In order to solve your issue you could apply a CSS class to the button and then use a class selector:
$('.someClass').on('click', function() {
...
});
Another possibility is to use the ends with selector:
$('[id$="btn_Insert"]').on('click', function() {
...
});
http://jagregory.com/writings/how-to-use-clientids-in-javascript-without-the-ugliness/
Just add ClientIDMode="Static" to your server button control and use its ID in js w/o problems:
$('btn_Insert').on('click', function() {
return Validate();
});