Objects in this mirror are closer to Microsoft Technologies. DNM objective is to help users on Microsoft technologies by providing Articles, snippets, Interview Questions.
Showing posts with label Java script. Show all posts
Showing posts with label Java script. Show all posts

12 September 2012

javascript not setting the hidden field value in ASP.NET


Today when i tried to change the value of hidden filed in java script, its not setting the value. I am using master page for an ASPX page.



<asp:HiddenField ID="hdnTest" runat="server" Value="Before JS Call" />



In java script method, when I try to assign the value it’s not getting replaced with new value.




document.getElementById('<%=hdnTest.ClientID%>').value = 'After JS Call';

then i replaced with below code

document.getElementById('ContentPlaceHolder1_
hdnTest').value = 'After JS Call';


and it worked out.

Not sure with the reason but its resolved!

02 September 2012

Change the text of a span control in javascript


In this tip we will see how to change the text of span control using Javascript. In HTML/ASPX page, span control will be defined as 
<span id="spanElement"> span text </span> 

In order to change the value of span element in java script use below code. 
document.getElementById('spanElement').innerHTML = "span text from JS";


31 August 2012

Remove special characters (!@#$%.etc) with empty string from a string using JavaScript


Using java script if you want to remove special characters from a string, we can achieve using regular expression.

The below code will allow alphabets (lower and upper case) and numbers in a string.
var str = '@This is sample sting from <.NET Mirror> Ready! 1#2%3*Go';
var rStr = str.replace(/[^a-zA-Z0-9]+/g, '');
alert(rStr);
Output : ThisissamplestingfromNETMirrorReady123Go

If you want allow some of special characters then add them to expression. In the below code we will try to add space and dot.  so now the expression allows alphabets, numbers space and dot.

var rStr1 = str.replace(/[^a-zA-Z0-9 .]+/g, '');
alert(rStr1);
Output: This is sample sting from .NET Mirror Ready 123Go