I am trying to make an image blink which is a part of my web part on sharepoint.
<asp:ImageButton ID="imgbtn1" onclick="imgbtn1_Click" ImageUrl="~/xxxxx.gif" runat="server" onload="Javascript:return blink();" />
function blink() {
var e = document.getElementById("imgbtn1");
e.style.visibility = (e.style.visibility == 'visible') ? 'hidden' : 'visible';
setTimeout("blink();", 500);
}
Whenever I try to load the page with the image, i get ) expected error. Is there something wrong with my syntax? please let me know...
Use OnClientClick instead of OnClick. OnClick is for server-side:
I think this should work:
<asp:ImageButton ID="imgbtn1" OnClientClick="blink(); return false;" ImageUrl="~/xxxxx.gif" runat="server" />
function blink() {
var e = document.getElementById("<%=imgbtn1.ClientID%>");
e.style.visibility = (e.style.visibility == 'visible') ? 'hidden' : 'visible';
setTimeout("blink();", 500);
}
Notice how I changed the part where you get a reference to imgBtn. You need to use <%=imgBtn.ClientID%>
EDIT
Apparently I misunderstood the purpose of your OnClick
This should do it then:
<asp:ImageButton ID="imgbtn1" OnClick="imgbtn1_Click" ImageUrl="~/xxxxx.gif" runat="server"/>
window.onload=blink;
function blink() {
var e = document.getElementById("<%=imgbtn1.ClientID%>");
e.style.visibility = (e.style.visibility == 'visible') ? 'hidden' : 'visible';
setTimeout("blink();", 500);
}
If you think doing window.onload=blink is a big deal, then do this on Page_Load:
Page_Load()
{
imgBtn.Attributes.Add("onload","blink();");
}
You can't use the OnLoad attribute of <asp:ImageButton /> to execute JavaScript because that is used for setting a server-side event handler for the control's Load event. Here's a workaround:
In your server-side Page_Load handler:
imgbtn1.Attributes["onload"] = "blink()";
Check this out
http://msdn.microsoft.com/en-us/library/asz8zsxy.aspx
http://forums.asp.net/t/1038225.aspx/1
http://msdn.microsoft.com/en-us/library/aa479011.aspx
http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientid.aspx
always use
"<%=id.ClientID%>"
when you use getelementbyid in JavaScript if you are combining the asp and JavaScript because asp will change the id at run time so the server element id will be different and the client id which you are expecting will be different.
Related
Fast and probably easy for you problem to solve.
function openNav() {
document.getElementById("myNav").style.visibility = "visible";
document.getElementById("myNav").style.opacity = "1";
document.getElementById("nav-icon3").attribute("onclick","closeNav()");
}
This function work fine with visibility and opacity but changing onclick function is not working. Any ideas?
Page: http://test.advermedia.pl/
Firstly, it's setAttribute(), not attribute(). Secondly, that's pretty moot anyway as using event attributes, let alone dynamically changing them at runtime, is a really bad idea. Use a single unobtrusive event handler instead:
<div id="nav-icon3">Toggle nav</div>
document.getElementById("nav-icon3").addEventListener('click', function() {
var nav = document.getElementById("myNav");
nav.style.visibility = nav.style.visibility == 'visible' ? 'hidden' : 'visible';
nav.style.opacity = nav.style.opacity == '1' ? '0' : '1';
});
Or alternatively, as you've tagged jQuery:
$('#nav-icon3').click(function() {
$('#myNav').toggle();
});
You have to set that attribute. Please try with the following:
document.getElementById("nav-icon3").setAttribute("onclick", "closeNav()");
I'm having some weird issues with the disabled attribute of an asp button and JavaScript, probably due to my lack of experience, but anyway...
As I understand from this question/answer, setting the disabled attribute makes the element disabled. Unless you set it like so; element.disabled = false;
So, that's what I do in my code;
<script type="text/javascript">
function CheckOne(inChkBox) {
... irrelevant code goes here ...
var rejectButton = document.getElementById('btnReject');
if (inChkBox.checked) {
rejectButton.disabled = false;
rejectButton.className = 'Enabled';
} else {
rejectButton.disabled = true;
rejectButton.className = 'Disabled';
}
}
</script>
<input type='checkbox' name='chkRejectLineItem' onchange='CheckOne(this);' runat="server"/>
This seems to trigger and work fine (the cssclass changes at the very least...) apart from the fact that my button is still disabled;
<asp:Button ID="btnProjectTimeReject" runat="server" Text="Reject Selected" OnClientClick="LineItemRejectReason();" OnClick="btnProjectTimeReject_Click" Enabled="False" CssClass="Disabled"/>
I know this, because the OnClientClick never gets called (for some reason, the server side code still gets called??). If I set the button to Enabled="True" then everything works as expected.
Is it because I'm using Enabled="True" initially and I should instead use rejectButton.removeAttribute('disabled') in the enable/disable JavaScript?
After a lot of console debugging and googling the same thing different ways, I found the following solution/answer.
var rejectButton = document.getElementById('btnPLReject');
if (inChkBox.checked) {
rejectButton.disabled = false;
rejectButton.className = 'Enabled';
$('#btnPLReject').bind("click", function() { LeaveRejectReason(); });
} else {
rejectButton.disabled = true;
rejectButton.className = 'Disabled';
}
This causes the button to trigger the JavaScript/Client side code AND then the Server side code...where as it was only doing the Server side code before...
I need some panadol now.
I have a link button in the page like:
<asp:LinkButton ID="edit" runat="server" OnClick="edit_Click" Enabled="False">ویرایش</asp:LinkButton>
I want Enable/Disable this in javascript.
I use this code but set visible
var objedit = document.getElementById('<%= edit.ClientID.ToString() %>');
objedit.style.display = "none";
I use this code but not enable
if (count == 1) {
objedit.disabled = false;
} else {
objedit.disabled = true;
}
I can click but link button is disabled.
This link should give you everything you need. You can't really "disable" a linkbutton because its just a link with some javascript attached. Basically, you need to reassign the click handler to something that returns void or false.
You can refer to the ID of the link with the following script:
<script runat="server">
var a = document.getElementById('<%= edit.ClientID.ToString() %>')
</script>
So, is this what you want?
http://jsfiddle.net/hzaR6/
http://jsfiddle.net/hzaR6/2/ -- UPDATED, tested in Chrome and Firefox
The UPDATED way
You can use a class name to define disabled element, for which you can have more control on their styles ... etc.
$("#link").toggleClass("disabled"); //This will simply toggle the class
and for the css
#link.disabled{
z-index:-1; /*Make it not clickable*/
position:relative;
opacity: .5; /*Lighter*/
}
You can do whatever you want here.
The good old form element way
$("#edit").attr("disabled", false);
-or-
document.getElementBy("edit").disabled = false;
This will disable any form element. If you want to enable them, just change false to true.
var a = document.getElementBy("edit").disabled;
a will be true if the element is disabled. Otherwise it will be false.
To disable element:
document.getElementById('<%# edit.ClientID %>').disabled = 'disabled';
//.ToString() is not necessary; ClientID is a string.
To re-enable:
document.getElementById('<%# edit.ClientID %>').disabled = '';
Of course, it can be done after document (DOM) is loaded.
try this
var objedit = document.getElementById("edit"); //not editid (editid is a variable)
objedit.disabled = true; //if I'm not mistaken its true/false or disabled
document.getElementById("lnk").style.display = "none";
Hi friends i have a button control which has to be enabled based on my asp.net checkbox check,but when run the code i am facing the problem my button is still disabled even after i perform a checkbox check .is their any property i have set in code behind to acccess the check event.
I am using the following code in my application
This is my javascript file
<script type="text/javascript">
function theChecker() {
var checkboxId = '<%= SpEligible.ClientID %>';
alert(checkboxId);
if (checkboxId.checked == true)
{
document.getElementById("SplistButton").disabled = false;
}
else
{
document.getElementById("SplistButton").disabled = true;
}
}
</script>
This is my code for the checkbox and button is
<asp:CheckBox ID="SpEligible" runat="server" Text="SpEligible" class="cBox" />
<asp:Button ID="SplistButton" runat="server" OnClientClick=" return ShowInsertForm()" Enabled="false"/>
This is my aspx.cs file where i am calling the javascript
SpEligible.Attributes.Add("onclick", "theChecker()");
I can see two major errors in your code :
1- In your <script> tag, you did realize that your Checkbox wouldn't have the same ID once in the page, but you didn't made that check. Also, as Ken Pespisa mentioned, the ID you took is only a string, therefore, it doesn't know any checked property. Here is the code I would write :
<script type="text/javascript">
function theChecker() {
var checkboxId = '<%= SpEligible.ClientID %>';
alert(checkboxId);
if (document.getElementById(checkboxId).checked == true)
{
document.getElementById("<%= SplistButton.ClientID %>").disabled = false;
}
else
{
document.getElementById("<%= SplistButton.ClientID %>").disabled = true;
}
}
</script>
2- In your .cs page, you don't seem to use any namespace. You may have hide some code, so I'll just say be sure to have namspaces and be sure to use this line inside a function, maybe the page load event function.
You need to change the javascript code to:
<script type="text/javascript">
function theChecker() {
var checkboxId = document.getElementById('<%= SpEligible.ClientID %>');
alert(checkboxId);
if (checkboxId.checked == true)
{
document.getElementById("SplistButton").disabled = false;
}
else
{
document.getElementById("SplistButton").disabled = true;
}
}
</script>
You were checking the checked property of a string constant. You need to get the control itself using document.getElementById which has checked property. I'd also rename "checkboxId" to "checkbox"
Change
document.getElementById("SplistButton")
to
document.getElementById(checkboxId)
<asp:CheckBox ID="htmlChkNotify" runat="server" OnCheckedChanged="ToggleTextBox(this,'htmlTxtNotifyemailaddress')" />
<asp:TextBox ID="htmlTxtNotifyemailaddress" runat="server" Enabled="false"></asp:TextBox>
function ToggleTextBox(CheckBox, TextBoxID) {
var TextBox = document.getElementById(TextBoxID);
if (CheckBox.checked) {
TextBox.disabled = false;
TextBox.value = "";
}
else {
TextBox.disabled = true;
TextBox.value = "";
}
}
I have write like but when I run the code then error message comeing " can't be pass this literal" something like that, so how can I do it?
Actually u can also see demo in stackoverflow "Notify daily of any new answers" I want to fire event like.
Thank you
OnCheckChanged is expecting an ASP.Net event handler, it's not for JavaScript, for that attach your event handler like this:
htmlChkNotify.Attributes.Add("onclick",
"ToggleTextBox(this,'" + htmlTxtNotifyemailaddress.ClientID + "');");
I broke it down to 2 lines for readability here only, but the idea is to generate an onclick inline handler, rather than the server-side event handler it's currently trying to bind, in a place it can't pass this (which is why the parser fails).