Hi I have a question regarding simple Javascript function. Below is the code its easily visible that when a user clicks on the click button I want to switch classes I mean when the page loads "This is para" needs to be displayed then when the user clicks on click button the text needs to change to This is updated. but dont know why its not working.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>untitled</title>
<script type="text/javascript" src="jquery.js"></script>
<style type="text/css">
.none {
display: none;
}
.display1 {
display: block;
}
</style>
</head>
<body>
<p id="default">This is para</p>
<p id="updated" style="display:none">This is updated</p>
<p><input type="button" value="Click" id="button1" /></p>
<script type="text/javascript">
$(document).ready(function(){
$("#button1").click(function(){
$("#default").addClass("none");
$("#updated").addClass("display1");
});
});
</script>
</body>
</html>
Do this instead:
$("#default").hide();
$("#updated").show();
And you can get rid of the none and display1 classes.
What's happening to you is that the style="display:none" takes precedence over a class, so it will never show that way.
Why are you not just using show() and hide()?
<script type="text/javascript">
$(document).ready(function(){
$("#button1").click(function(){
$("#default").hide();
$("#updated").show();
});
});
</script>
If you just want to change the text, instead of having two elements, why not remove the updated element and use .text(), like so:
<script type="text/javascript">
$(document).ready(function(){
$("#button1").click(function(){
$("#default").text("This is updated");
});
});
</script>
Your "addClass" call is (probably) working, but the style directly on the element trumps the style offered by the CSS.
Personally I prefer to do that sort of thing by changing "class" values like that, but you can't really mix and match the two approaches. (Well, you can, but it's confusing and error-prone.) Instead of starting your element with that style, give it instead your "none" class.
Related
Spent hours on this and don't understand that behavior. Any advice would be appreciated.
If you look below you load this code you will see the toolbar for sharing with add to any plugin that is what the page.js is including control for.
You can see I have commented out a foo bar at the bottom. That works just fine after load. I hit the button and the foo turns into a bar. When I load the entire div though I am trying to change the text ultimately by what I put in the insert form. An odd thing happens the entire add to any button dissapears. Any idea why? Is the page reloading and cannot grab the .js file or something bizarre like that?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript" src="//static.addtoany.com/menu/page.js"></script>
<script>
function second(){
//document.getElementById('hello').innerHTML = 'bar';
document.getElementById('hello').innerHTML = '<div class=\"a2a_kit a2a_kit_size_32 a2a_default_style\"> <a class=\"a2a_dd\" href=\"https://www.addtoany.com/share\"></a></div>';
};
</script>
</head>
<body>
<form>
<input type="text" name="foo" id="foo"></input>
<input type="button" onClick="second();first();"></input>
</form>
<div id="hello"></div>
</body>
<script type="text/javascript">
document.getElementById('hello').innerHTML = '<div class=\"a2a_kit a2a_kit_size_32 a2a_default_style\"><a class=\"a2a_dd\" href=\"https://www.addtoany.com/share\"></a></div>';
//document.getElementById('hello').innerHTML = 'foo';
</script>
</html>
Hi I'm trying to use jQuery to load an html document into an existing html document.
I've tried using the code below, but the text doesn't load.
I'm not sure why. Could someone point me towards what I'm doing wrong please?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">$("#test").load("test.txt")</script>
</head>
<body>
<div id="test"></div>
</body>
</html>
Try on DOM ready like
<script type="text/javascript">
$(document).ready(function(){
$("#test").load("test.txt");
});
</script>
And you also forgotted ending ;.You can also try like
$(function(){
$("#test").load("test.txt");
});
You need to add it in dom ready
jQuery(function($){
$("#test").load("test.txt")
})
The problem was when your script is executed the element with id test was not yes added to the dom so the selector $("#test") would return zero elements
I am trying to delete the first css rule.
However, this does not work in Firefox 3.6.28. Is this the same for everyone and why is this the case?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" >
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
$(window).load(function(){
console.log(document.styleSheets);
document.styleSheets[0].deleteRule(0);
console.log(document.styleSheets); // same as the first console.log
});
</script>
<style>
body{font-size:62.5%;}
#mc{padding:310px 0 10px 162px; color:#fff;}
#content{width:500px;}
#header{padding-top:10px;}
#banner{padding:10px 0}
</style>
</head>
<body>
Test JS
</body>
</html>
Update
Just to clarify, the delete seems to be working however the console.log shows the same number of rules before and after the delete.
Seems to work just fine for me
example at http://jsfiddle.net/gaby/JmWwM/
you need to check the length of the cssRules property.
If you log the actual object in firebug then when you try to dig deeper it will show you the current state of the stylesheet, so it will have a reduced number of rules.. (since by the time you start digging the deletion will have occured)
but if you log the actual number of rules before and after the deletion then it will show the correct results..
so the problem is a mistake in understanding how the console.log command works.
According to https://developer.mozilla.org/en/DOM/CSSStyleSheet/deleteRule stylesheet.deleteRule(index) , delete rule accepts an index so your 1 should be a 0.
I created a jsfiddle http://jsfiddle.net/N3zsw/. I had to make some changes to your code since there are other stylesheets on the page so 0 == 3.
It appears to work for me. When you do console.log you should be displaying console.log(document.styleSheets[0]) instead of console.log(document.styleSheets). The CSS
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" >
<style>
body{font-size:62.5%;}
#mc{padding:310px 0 10px 162px; color:#fff;}
#content{width:500px;}
#header{padding-top:10px;}
#banner{padding:10px 0}
</style>
<script type="text/javascript">
(function () {
var s = document.styleSheets[0];
console.log(s.cssRules.length);
s.deleteRule(1);
console.log(s.cssRules.length);
s.deleteRule(1);
console.log(s.cssRules.length);
s.deleteRule(1);
console.log(s.cssRules.length);
s.deleteRule(1);
console.log(s.cssRules.length);
})();
</script>
</head>
<body>
Test JS
</body>
</html>
it is working fine, i think its about firebug's object reference thing
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
One function to toggle multiple hidden elements?
I saw this working in a jsFiddle but for whatever reason I can't get it to work. The code is the way it is because I will have 100+ links from which to choose and I didn't want to make 100 different slideToggle functions.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<head>
<script type="text/javascript" src="jquery.js">
</script>
<script type="text/javascript">
$('.county').click(
function(){
var thisIs = $(this).index();
$('.countystats').eq(thisIs).slideToggle(300);
});
</script>
<style type="text/css">
.county{ font:"Trebuchet MS", Arial, Helvetica, sans-serif;
}
.countystats{
background-color:blue;
display:none;
}
</style>
</head>
<body>
<div>
<a class="county" href="javascript:;">one</a>
<a class="county" href="javascript:;">two</a>
</div>
<div class="countystats">stats one</div>
<div class="countystats">stats two</div>
<br />
<br/>
</body></html>
working jsFiddle url: http://jsfiddle.net/davidThomas/ANu83/
I would also recommend changing the .click to a .live to allow any dynamically added content to also honor the click.. and wrap it in a document.ready so it doesn't matter where in the document the script is.
$(document).ready(function(){
$('.county').live('click',function(){
var thisIs = $('.county').index($(this));
alert(thisIs);
$('.countystats').eq(thisIs).slideToggle(300);
});
});
The solution given to you previously relies on two things.
The links are nested in a specific order within a single div having no elements between them (ie <br/>)
The countystats divs immediately follow the div that contains the links.
If it still doesn't work it would be much more helpful to post a your non-working code than the working example.
Working version follows. Your code has two issues
The code that is supposed to "go through all of the things that are class=county and assign an event handler" executes before any of those things are defined. So it finds none. I moved that script to a position after those tags are created.
The line in the event handler that does var thisIs = $(this).index(); - I have no idea why this works in jsfiddle (maybe a different version of jquery?) It doesn't work at all in my browser. I rewrote it slightly below.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<style type="text/css">
.county{ font:"Trebuchet MS", Arial, Helvetica, sans-serif;}
.countystats{
background-color:blue;
display:none;
}
</style>
</head>
<body>
<div>
<a class="county" href="javascript:;">one</a>
<a class="county" href="javascript:;">two</a>
</div>
<div class="countystats">stats one</div>
<div class="countystats">stats two</div>
<script type="text/javascript">
$('.county').click(
function(){
var thisIs = $('.county').index(this);
$('.countystats').eq(thisIs).slideToggle(300);
});
</script>
</body></html>
On button click, I want to hide the div. How do i do it?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<script language="javascript" type="text/javascript">
function button()
{
var a = document.getElementById('approve');
document.getElementById('p').innerHTML= 'Fred Flinstone';
}
</script>
<body>
<div id="hide">
<form>
<p id="p">heya</p>
<input type="button" id='approve' value="approve" onclick="button()"/>
<input type="button" id="reject" value="reject"/>
</form>
</div>
</body>
</html>
SORRY FOR ASKING AGAIN...BUT COULDN'T FIND A BETTER METHOD. THANKS
document.getElementById('hide').style.display = 'none';
This sets the CSS code display: none; on the div, which causes the browser's rendering engine to act like the element is not even on the page (e.g. it does not occupy any space). See the CSS standard for the official description.
You can just use Jquery to make things super easy:
$("#approve").click( function() {
$("div#hide").hide() //you can just use $("#hide") if you want
});
You can show the element whenever you want with $("div#hide").show(). This will save you from having to deal with browser inconsistencies and other crap so you can just focus on producing great readable code. :)
If you use jQuery:
$(document).ready(function() {
$('#approve').click(function() {
$('#hide').hide();
});
});