Changing error with javascript - javascript

I want to change 'Enter text here' to 'something else'.
I cannot access to server files, so I need to do it with a javascript-code. The following code is not working:
<script language="JavaScript">
$(function() {
$(".ws-po-box:contains('enter text here')").text('something else');
});
</script>
EDIT: Typo only when asking, not in code.

Looks like a typo in your syntax. Try:
<script language="JavaScript">
$(function() {
$(".ws-po-box:contains('Enter text here')").text('something else');
});
</script>

Use jQuery.filter for selecting case-insensitive content as :contains selector deals with case-sensitive textContent
$(function() {
$(".ws-po-box").filter(function() {
return this.textContent.toLowerCase() === 'enter text here';
}).text('something else');
});

Related

js not working in asp.net

i am trying to change text of label and textbox text on checkbox clicked.
i have noticed that asp.net fully support js .
<script type="text/javascript">
$(document).ready(function () {
$('#chkhtml').change(function () {
$('#Text1').prop("disabled", !$(this).is(':checked'));
document.getElementById('#Text1').innerHTML = 'newtext';
document.getElementById('#<%=rollLbl.ClientID %>').innerHTML = 'your text goes here';
document.getElementById('#testlbl').innerHTML = 'newtext';
});
});
</script>
However it only enable or disable text box but don't change text of any label or text box.
i have already tried .value .content etc but none of these work plzzz plzz help me .
You have mixed up jQuery and Javascript:
jQuery
<script type="text/javascript">
$(document).ready(function() {
$('#chkhtml').change(function() {
$('#Text1').prop("disabled", !$(this).is(':checked'));
$('#Text1').val('newtext');
$('#<%=rollLbl.ClientID %>').html('your text goes here');
$('#testlbl').html('newtext');
});
});
</script>
Please amend below changes in your code, if you want to keep original.
<script type="text/javascript">
$(document).ready(function() {
$('#chkhtml').change(function() {
$('#Text1').prop("disabled", !$(this).is(':checked'));
document.getElementById('Text1').value = 'newtext';
document.getElementById('<%=rollLbl.ClientID %>').innerHTML = 'your text goes here';
document.getElementById('testlbl').innerHTML = 'newtext';
});
});
</script>
You need to change
document.getElementById('#Text1')
to
document.getElementById('Text1')
And the rest of such calls too. getElementById accepts an ID, not a selector.
you may try in jquery
$('#Text1').html('newtext');
$('#<%=rollLbl.ClientID %>').html('your text goes here');
or in plain javascript
document.getElementById('Text1').innerHTML = 'newtext';
document.getElementById('<%=rollLbl.ClientID %>').innerHTML = 'your text goes here';
you need to pass only the id of the element to getelementById(). but jquery uses css query selector so #name means it is an id and .name means it is a class

Add in onclick the script type

I need to call the method _trackingEvent if the button was clicked, So I added in the onlick in the button the following code :
<button class="btnInscription"
onclick="<script type="text/javascript">
$(function () {GA._trackEvent('Account','Subscribe','Not Facebook');});
</script>;">
when I look in the source of page all code that I put it up is whith red color. I think the problem is with parenthesis.
I see 2 problems :
you have unescaped " inside your attribute
you don't need the tag inside onclick ( onclick code is always javascript code)
the following code should work :
onclick="$(function () {GA._trackEvent('Account','Subscribe','Not Facebook');});"
don't do thinks like this!
Better this way:
<button class="btnInscription">blub</button>
<script>
$('.btnInscription').on('click', function() {
GA._trackEvent('Account','Subscribe','Not Facebook');
});
</script>
make sure to launch JS at the Bottom of your page
You are miss placing it.
The script tag should be in the head of the page and the onclick attribute should call the function :
//this should be in the <head> of the page, inside a <script> tag
function track() {
//$(function () {GA._trackEvent('Account','Subscribe','Not Facebook');});
console.log('executed when clicked');
}
<button class="btnInscription"
onclick="track()">test</button>
Also Try Once In Javascript
function testfun() {
//$(function () {GA._trackEvent('Account','Subscribe','Not Facebook');});
console.log('If Click == true then it will execute');
}
<button class="btnInscription"
onclick="testfun()">click</button>
try it Once In your File
<button class="btnInscription" id="my-btn">click</button>
<script>
$(dicument).ready(function(){
$('#my-btn').click(function(){
alert('clicked for testing');
{GA._trackEvent('Account','Subscribe','Not Facebook');}
});
});
</script>

Some help explaining javascript function

would you explain please, why this code is showing alert window with the text at the end of radio element:
<script>
function markReply(el){
alert(el.nextSibling.nodeValue);
}
$(document).ready(function(){
markReply();
});
</script>
and this one does not:
<script>
function markReply(el){
return el.nextSibling.nodeValue;
}
$(document).ready(function(){
var msg = markReply();
alert(msg);
});
</script>
there are 4 optional answers selected by radio element, like:
<input type="radio" name="choise" onclick="markReply(this);"/>....some text
Thank you!
The second script just returns the value, without doing anything with it.
Note that in both cases, the call from the document ready function is useless, and probably produces an el is not defined error in the console.
You're not passing any parameters to the markReply() function when you created a variable for it.
Try this:
var msg = markReply(el);
The script that does not alert the message is not working because the parameter of the function markReply is not there when the jquery is loaded.
HTML
<input type="radio" name="choise" id="choise"/>....some text
JS
$(document).ready(function(){
$("#choise").click(function () {
alert(markReply(this));
});
});
function markReply(el){
return el.nextSibling.nodeValue;
}
http://jsfiddle.net/j4sgdton/

Change label text using JavaScript

Why doesn't the following work for me?
<script>
document.getElementById('lbltipAddedComment').innerHTML = 'Your tip has been submitted!';
</script>
<label id="lbltipAddedComment"></label>
Because your script runs BEFORE the label exists on the page (in the DOM). Either put the script after the label, or wait until the document has fully loaded (use an OnLoad function, such as the jQuery ready() or http://www.webreference.com/programming/javascript/onloads/)
This won't work:
<script>
document.getElementById('lbltipAddedComment').innerHTML = 'your tip has been submitted!';
</script>
<label id="lbltipAddedComment">test</label>
This will work:
<label id="lbltipAddedComment">test</label>
<script>
document.getElementById('lbltipAddedComment').innerHTML = 'your tip has been submitted!';
</script>
This example (jsfiddle link) maintains the order (script first, then label) and uses an onLoad:
<label id="lbltipAddedComment">test</label>
<script>
function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function() {
if (oldonload) {
oldonload();
}
func();
}
}
}
addLoadEvent(function() {
document.getElementById('lbltipAddedComment').innerHTML = 'your tip has been submitted!';
});
</script>
Have you tried .innerText or .value instead of .innerHTML?
Because a label element is not loaded when a script is executed. Swap the label and script elements, and it will work:
<label id="lbltipAddedComment"></label>
<script>
document.getElementById('lbltipAddedComment').innerHTML = 'Your tip has been submitted!';
</script>
Use .textContent instead.
I was struggling with changing the value of a label as well, until I tried this.
If this doesn't solve try inspecting the object to see what properties you can set by logging it to the console with console.dir as shown on this question: How can I log an HTML element as a JavaScript object?
Here is another way to change the text of a label using jQuery:
<script>
$("#lbltipAddedComment").text("your tip has been submitted!");
</script>
Check the JsFiddle example
Using .innerText should work.
document.getElementById('lbltipAddedComment').innerText = 'your tip has been submitted!';
Try this:
<label id="lbltipAddedComment"></label>
<script type="text/javascript">
document.getElementById('<%= lbltipAddedComment.ClientID %>').innerHTML = 'your tip has been submitted!';
</script>
Because the script will get executed first.. When the script will get executed, at that time controls are not getting loaded. So after loading controls you write a script.
It will work.

Regular Expression: replace everything but not a specific word

I'm trying to perform a simple replace(); method in JavaScript where I'd like to remove every class from an HTML element except a specific one, how can I do it?
I tried this without success:
<div id="my_div" class="hello letsgo baby is_checked cool">Gordon Freeman</div>
<script>
$(document).ready(function () {
alert ($(#my_div).attr("class").replace (/^(is_checked)$/, ""));
// id'like it returns "is_checked" or "" to work like a boolean;
});
</script>
You're trying to alert is_checked when it's there, right? Try using .hasClass:
$(document).ready(function () {
if($("#my_div").hasClass("is_checked")) {
alert("is_checked");
}
else {
alert("");
}
});
If you just want to know whether an element has a certain class on it, this returns true or false telling you exactly that:
$("#my_div").hasClass("is_checked")
$(document).ready(function () {
if ($(this).hasClass('is_checked') {
$(this).removeClass();
$(this).addClass('is_checked');
}
});
if ($(this).hasClass('is_checked'))
$(this).removeClass().addClass('is_checked');
else
$(this).removeClass();
You want to remove every class except a specific one? You can use jQuery's attr() method and define the class specifically:
$('#my_div').attr('class','is_checked');
Here's an example of this in action: http://jsfiddle.net/decHw/
Also important to note that if you wish to select an element in jQuery by its id, you need to wrap it in quotes. You have:
$(#my_div)
It should be:
$('#my_div')

Categories

Resources