Helo,
I'm working with external software which is generating reports.
I'm getting table, then it is printed to website in div.
I don't have access to this table before it is generated, so i can't set any attribute before website is rendered.
So I need to add attribute to this table as last step of rendering process, it doesn't matter is it ID or Class.
Structure is like:
<div class="data" id="Checklist">
<p>Some text</p>
<!-- There is this table -->
<table style="...">...</table>
<p></p>
</div>
I'm using IE v11.
I tried something like this (nothing happens):
document.getElementById("Checklist").childNodes[0].className = "TestClassName";
Also (it gives mi error: Object doesn't support property or method 'setAttribute' )
document.getElementById('news').childNodes[0].setAttribute( 'class', new_class );
Any other ideas?
If you use ChildNodes it will return all the white space with nodelist so use children so that it will return only child elements
<div class="data" id="Checklist">
<p>Some text</p>
<!-- There is this table -->
<table style="...">...</table>
<p></p>
</div>
Change Your js to
document.getElementById("Checklist").children[0].className="TestClassName";
document.getElementById('news').children[0].setAttribute( 'class', new_class );
it will work
try this to add class
document.getElementById("Checklist").classList.add("TestClassName");
document.getElementById('Checklist').childNodes[1].setAttribute('class', 'table1');
.TestClassName {
color: red;
}
.table1 {
border: solid 1px;
}
<div class="data" id="Checklist">
<p>Some text</p>
<!-- There is this table -->
<table style="...">...</table>
<p></p>
</div>
Try this
<div class="data" id="Checklist">
<p>Some text</p>
<!-- There is this table -->
<table style="...">...</table>
<p></p>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$("#Checklist").children('table').attr("class", "class_name");
</script>
change class_name with your class name
Assuming this is your code,
<div class="data" id="Checklist">
<p>Some Text</p>
<div id="WhereTableWillGo">
<table></table>
</div>
<p></p>
</div>
You could do the alterations within a window.onload function,
window.onload = function() {
document.getElementById("Checklist").getElementsByTagName("table")[0].classList.add("NewClass");
// OR
document.getElementById("Checklist").getElementsByTagName("table")[0].setAttribute("class", "TestClassName");
}
Or you could fetch the table asynchronously by doing the following, and do your alterations before inserting the table,
var xhttp = new XMLHttpRequest;
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
// Do Alterations to Table
document.getElementById("WhereTableWillGo").innerHTML = xhttp.responseText;
}
}
xhttp.open("GET", "https://website.com");
xhttp.send();
Related
I have multiple divs with id='title'.
When that div is hovered over I want a hidden div with id="source" to appear.
I am using the following code but only the first div with id='title' on the page works.
<!-- Show Source / Date on mouse hover -->
<script>
$(document).ready(function(){
$("#title").mouseenter(function(){
$("#source").fadeIn( 200 );
});
$("#title").mouseleave(function(){
$("#source").fadeOut( 200 );
});
});
</script>
HTML Example:
<div id="title" class="col-md-3">
<div id="source" style="display:none">Hidden Text</div>
</div>
<div id="title" class="col-md-3">
<div id="source" style="display:none">Hidden Text</div>
</div>
<div id="title" class="col-md-3">
<div id="source" style="display:none">Hidden Text</div>
</div>
Use classes instead of IDs
use .find() to search for descendants of an element
jQuery(function($) { // DOM ready and $ alias in scope
$(".title").on({
mouseenter() {
$(this).find(".source").fadeIn(200);
},
mouseleave() {
$(this).find(".source").fadeOut(200);
}
});
});
.title {
display: flex;
}
/* Utility classe */
.u-none {
display: none;
}
<div class="title col-md-3">
TITLE 1
<div class="source u-none">Visible Text</div>
</div>
<div class="title col-md-3">
TITLE 2
<div class="source u-none">Hidden Text</div>
</div>
<div class="title col-md-3">
TITLE 3
<div class="source u-none">Hidden Text</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
Hello Δce Let's start by modifying the source code to use classes for you to pick. This helps us describe the functionality and allow the ID's to serve their purpose.
we'll take your
<div class="col-md-3 show-source-on-hover">
<div class="source" style="display:none">Hidden Text</div>
</div>
<div class="col-md-3 show-source-on-hover">
<div class="source" style="display:none">Hidden Text</div>
</div>
<div class="col-md-3 show-source-on-hover">
<div class="source" style="display:none">Hidden Text</div>
</div>
and we can update the jQuery code
<script>
$(function() {
$(".show-source-on-hover")
.mouseenter(function() {
$(this).find('.source').fadeIn( 200 );
})
.mouseleave(function() {
$(this).find('.source').fadeOut( 200 );
});
});
</script>
You can see here there's the use of this wrapped in the $() jquery object function. and the new use of .find to get get the correct source class.
For a working demo please see the following link: https://codepen.io/jessycormier/pen/GRmaaEb
Note: your use of ID's should change and always try and be unique like others have suggested. MDN (Mozilla Developer Network) has some great documentation on the standards of HTML, CSS, and JavaScript which can help give you.
Also, in the demo I've given your column divs some default size otherwise there is nothing to trigger the mouse over events. Good luck and happy coding 🚀
IDs are IDENTIFIERS. What that means is that there can only be one per document. Classes are not unique, so you can use them.
✅ $(".title")
❌ $("#title")
the idea is list and grid classs.
example code:
$(document).ready(function(){
$(".cfredcf").on('click', function() {
$('.course-title-cf').removeClass('cfbluecf-p');
$('.course-title-cf').addClass('cfredcf-p');
});
$(".cfbluecf").on('click', function() {
$('.course-title-cf').removeClass('cfredcf-p');
$('.course-title-cf').addClass('cfbluecf-p');
});
});
.cfredcf-p{
color:red;
}
.cfbluecf-p{
color:blue;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p class="cfredcf" style="color:red;">red</p>
<p class="cfbluecf" style="color:blue;">blue</p>
<hr />
<p class="course-title-cf">Title Example</p>
<p class="course-title-cf">Title Example</p>
<p class="course-title-cf">Title Example</p>
<hr />
the code working fine until now but when get more (Title Example) from the database by ajax and already choose red the new results is not coming with red i must click on red again !.
the new title or results comming from database:
it will be like that
<hr />
<p class="course-title-cf">Title Example</p>
<hr />
but not geting withe new add class i must click agin to add.
In fact, it would be more effective to add (AJAX) codes and see the scenarios, but,
A lot of solutions come to mind, but if you ask what is the smartest, fastest and easiest one, I would make a <div> element that wraps all these <p> elements. And until I color each <p>, I would do <div class = "cfredcf-p / cfbluecf-p">. Then my css is .cfredcf-p p {color: red;} I would rearrange it as.
$(document).ready(function() {
$(".cfredcf").on('click', function() {
$('.course-title-cf').removeClass('cfbluecf-p');
$('.course-title-cf').addClass('cfredcf-p');
});
$(".cfbluecf").on('click', function() {
$('.course-title-cf').removeClass('cfredcf-p');
$('.course-title-cf').addClass('cfbluecf-p');
});
$("button").on('click', function() {
$('.course-title-cf').append("<p>Title Example</p>");
});
});
.cfredcf-p p {
color: red;
}
.cfbluecf-p p {
color: blue;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p class="cfredcf" style="color:red;">red</p>
<p class="cfbluecf" style="color:blue;">blue</p>
<button>Add new</button>
<hr />
<div class="course-title-cf">
<p>Title Example</p>
<p>Title Example</p>
<p>Title Example</p>
</div>
<hr />
Thus, every new <p> element inserted inside the wrapper element will take the same color.
I want to have a simple read more and read less button functionality where if one button is clicked then it will show the text and when another is clicked it will hide the previously opened and so on but I have been unsuccessful in changing the button text as each is changed e.g. when it says read more and when I click it changes to read less, then when I click another button it is still displaying read less.
Here is my example of what I have done so far https://codepen.io/Niall_Caffrey/pen/abNavaj
$('body').on('click', '.shownow', function() {
$(this).parents('.main').find('.notshow').addClass('nohide').toggle();
$('.notshow').each(function() {
if (!$(this).hasClass('nohide')) {
$(this).hide();
} else {
$('.notshow').removeClass('nohide');
}
});
if($(".notshow").is(":hidden")){
$(this).text("Read More");
$('.notshow').addClass('showhide');
$('.main').addClass('showhide');
};
if($(".notshow").is(":visible")){
$(this).text("Read Less");
$('.notshow').removeClass('showhide');
$('.main').removeClass('showhide');
};
});
.notshow {
display: none;
}
.main{
width:25%;
float:left;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h3>How it is done</h3>
<div class="main">
<div class="notshow">
<p>Lorem Ipsum
</p>
</div>
<div class="title">
<div class="shownow">
<p class="btn">Read More</p>
</div>
</div>
</div>
<div class="main">
<div class="notshow">
<p>lorem Ipsum</p>
</div>
<div class="title">
<div class="shownow">
<p class="btn">Read More</p>
</div>
</div>
</div>
<div class="main">
<div class="notshow">
<p>Lorem ipsum</p>
</div>
<div class="title">
<div class="shownow">
<p class="btn">Read More</p>
</div>
</div>
</div>
The problem is how to make clickable/togglable buttons that correctly both hide the text and relabel the button of any previously clicked button.
I am not familiar with jquery and I couldn't completely follow the given code, except to note that the writing of 'Read More' was going to 'this' which is the most recently clicked element, not the previous one. I also noted that the way 'Read More/Less' was being written using jquery text overwrote the button (a p element) so it was only there up until the user did the first click.
Keeping the structure of HTML but replacing the onclick function with the code below seems to work. This is deliberately written in naked JavaScript so as not to introduce errors such as the use of text in the original.
var previousClicked='';
$('body').on('click', '.shownow', function() {
if (previousClicked!=this) {
hideTextEl(previousClicked);
}
previousClicked=this;
if (findTextEl.classList.contains('notshow')) {showTextEl(this);
} else {hideTextEl(this);}
function hideTextEl(el){
if (el) {
el.firstElementChild.innerHTML='Read More';
findTextEl(el).classList.add('notshow');
}
}
function showTextEl(el){
if (el) {
el.firstElementChild.innerHTML='Read Less';
findTextEl(el).classList.remove('notshow');
}
}
function findTextEl(el) {
while (!el.classList.contains('main')) {el=el.parentElement;}
return el.firstElementChild;//NB if the structure of the HTML changes this may need changing
}
});
I recently start to create an application with phonegap and jquery mobile that loads a json an create a elements this is my code.
<script type="text/javascript">
$(document).ready(function(){
$("#boton").click(function(){
$.getJSON("json url",function(data){
var instrucciones = data.Instrucciones;
document.getElementById("titulos").innerHTML = instrucciones;
var acordeonJSON = $('<div data-role="collapsible" id="accordeon"><h3>hi</h3><p>Hello</p></div>');
$.each(data.Items,function(llave,valor){
if(valor.nombreItem !="")
{
$("#accordeon").append(acordeonJSON);
}
});
});//Llave getJSON
});//Llave boton click
});
</script>
This is my Html structure
<div data-role="page" id="home">
<div data-role="header" id="titulo">
<h1 id="titulos"></h1>
</div>
<div data-role="main" class="ui-content" id="div1">
<button id="boton">Presioname</button>
<div id="instrucciones"></div>
<div data-role="collapsible-set" data-theme="c" id="accordeon">
<!--In this part i want to show the content of my json-->
</div>
</div>
<div data-role="footer" data-position="fixed">
<h1>Footer</h1>
</div>
</div>
My question is why i cant create my elements in jquery, the created elements shows without style.
After appending all the new dom elements within the collapsibleset, call $("#accordeon").collapsibleset( "refresh" ).enhanceWithin();
For example:
$("#boton").click(function(){
var acordeonJSON = $('<div data-role="collapsible" id="accordeon1"><h3>hi</h3><p>Hello</p></div>');
$("#accordeon").append(acordeonJSON).collapsibleset("refresh").enhanceWithin();
});
Here is a DEMO
<script type="text/javascript">
//slides the element with class "menu_body" when paragraph with class "menu_head" is clicked
var olditemclicked = null;
$("#firstpane p.menu_head").click(function()
{
if(olditemclicked !== null){
olditemclicked.css({backgroundImage:"url(./javascript/modules/baseModule/theme/standard/img/left.png)"});
}
$(this).css({backgroundImage:"url(./javascript/modules/baseModule/theme/standard/img/down.png)"}).next("div.menu_body").slideToggle(300).siblings("div.menu_body").slideUp("slow");
olditemclicked = $(this);
});
</script>
<div id="firstpane" class="menu_list"> <!--Code for menu starts here-->
<p class="menu_head_home" style="border-top-left-radius:2px;"><span class="home_disp">Home</span></p>
<p class="menu_head" style="border-top-left-radius:2px;">System</p>
<div class="menu_body">
Providers
Zones
Image Repository
</div>
<p class="menu_head">Service</p>
<div class="menu_body" style="display:block">
Instances
Service Group
</div>
<% if(isAccessValid('UsersManagement')) { %>
<p class="menu_head">User Management</p>
<div class="menu_body">
User
</div>
<% } %>
<p class="menu_head">Monitoring</p>
<div class="menu_body">
Alarms
Threshold
</div>
<!-- <p class="menu_head">MAINTENANCE</p>
<div class="menu_body">
Logs
Migration
</div> -->
<p class="menu_head">Settings</p>
<div class="menu_body">
Platform Settings
Application Settings
</div>
</div>
Please let me get an answer?
It looks like you haven't wrapped any of the code that references the DOM (e.g. $("#firstpane p.menu_head")) within a $(document).ready... Try wrapping it within the following and see whether you have more success.
<script type="text/javascript">
$(document).ready(function() {
// Code referencing DOM here
});
</script>