How to create element with class name using javascript ?
i tried to create element like this
<div class ="dialog_red_top_page">
<div class ="inner_dialog_red_top_page">
<p class="dialog_red_top_page_text">
test
</p>
</div>
</div>
by using javascript but not work , how can i do that ?
http://jsfiddle.net/n5phf/186/
<script type="text/javascript">
$(window).load(function(){
$('#myBtn').click(function(){
var div = $('<div class ='dialog_red_top_page'><div class ='inner_dialog_red_top_page'><p class='dialog_red_top_page_text'>test</p></div></div>');
div.html("test");
div.appendTo('#wrapper');
});
});
</script>
1st : the problem with quotes use " around class instead of single one
2nd : with div.html("test"); you change the div html to <div>test</div>
you can use .html() for wrapper
$('#myBtn').click(function(){
var div = $('<div class ="dialog_red_top_page"><div class ="inner_dialog_red_top_page"><p class="dialog_red_top_page_text"">test</p></div></div>');
$('#wrapper').html(div);
// you can try .append instead of .html() just give it a try
//$('#wrapper').append(div);
//$(div).appendTo('#wrapper');
});
Working Demo
Your quotes are all singel quotes
Do it like this
var div = $('<div class="dialog_red_top_page">'
+ '<div class="inner_dialog_red_top_page">'
+ '<p class="dialog_red_top_page_text">test</p>'
+ '</div></div>'
);
Wrap the classes name by " instead of ' and remove div.html("test");. It says replace html of selected element by test it means it removes .inner_dialog_red_top_page and replace it by test.
Jsfiddle
Escape all ' characters with a \':
var div = $('<div class =\'dialog_red_top_page\'><div class =\'inner_dialog_red_top_page\'><p class=\'dialog_red_top_page_text\'>test</p></div></div>');
What your code actually does is: creates a div with div and paragraph inside, but then you call:
div.html("test");
which overrides content of .dialog_red_top_page div.
jsfiddle
your code should be like this:
$('#myBtn').click(function(){
$("<div class ='dialog_red_top_page'><div class ='inner_dialog_red_top_page'><p class='dialog_red_top_page_text'>test</p> </div></div>").appendTo('#wrapper');
});
Related
Here is my markup :
<div class="section-dots active-section"></div>
<div class="section-dots"></div>
<div class="section-dots"></div>
<div class="section-dots"></div>
Here is my JavaScript code:
Note, I am building my markup with the following Jquery code, dynamically:
<script>
var count = 1;
var next;
var url;
var elementId;
// First remove all of the dots
$('.section-dots-container').empty();
$('H2').each(function () {
elementId = 'section-' + count;
url = '#section-' + count;
$(this).attr('id', elementId);
$('.section-dots-container').append('<DIV Class="section-dots"></DIV>');
count++;
});
</script>
This is my script for the click event:
<script>
function setActive() {
next = $(this).find('.section-dots');
next.addClass("active-section");
}
</script>
thus it is setActive that should add "active-section" to the Div inside the anchor tag which I am building dynamically. but I not certain if I am doing this correctly.
It seems like it would be easier to just bind the click event to your element in your js code based on its class rather than adding it into your html.
So maybe change markup to include a common class name like:
<div class="section-dots active-section"></div>
<div class="section-dots"></div>
<div class="section-dots"></div>
<div class="section-dots"></div>
And then just bind a click event to all elements with that class:
$('.section').click(function(){
$(this).find('.section-dots').addClass("active-section");
});
I would un-capitalize the DIV and some other things in your code, but this should work if I'm understanding what you want to do.
$('.section-dots').click(function(){
$(this).addClass('active-section');
});
you are using $(this) without passing any parameter to the function.
<script>
function setActive() {
next = $(this).find('.section-dots');
next.addClass("active-section");
}
</script>
useing jquery, select the element you want to add the class to it and use the addClass("class_to_add") function.
I have something like shown below.
<div class="alert alert-info" style="width:224px">
<strong>Info!</strong> This is a netural alert
</div>
Through javascript or jquery, at certain condition, I want to change the class of that div element from alert alert-info to alert alert-success. Is that possible? And if so, how?
Change class with condition, Use the following sample
$('.alert').click(function(){
if(0==0)//your condition goes here
{
$(this).removeClass('alert-info').addClass('alert-success');
}else{
$(this).removeClass('alert-success').addClass('alert-info');
}
});
you can use:
document.getElementById("myElementId").className = "MyClass";
use space-delimited list to apply multiple classes,if you want to add class here is an example:
document.getElementById("myElementId").className += " MyClass";
var divArray = document.getElementsByClassName("alert-info");
var div = divArray[0];
div.className = "alert alert-success";
With jQuery....
$('.alert').removeClass('alert-info').addClass('alert-success');
.. Unless I'm missing something.
I'm trying to add a class to a div by using the title attribute. Currently the alert is correct. However the class isn't added.
JS Part:
function index(clicked_id){
alert(clicked_id);
$('#sticky').attr("title", +clicked_id).addClass("glow");
}
HTML Part:
<div id="sticky" class="" title='sticky1' onclick='index(this.title)'</div>"
I don't know if I got your question right but I understood that you want to filter|find you div by the title. So maybe this code will help you:
function index(clicked_id){
alert(clicked_id);
$('#sticky [title="' + clicked_id + '"]').addClass("glow");
}
This is how I would do it:
$("[title*='sticky']").click(function(){
$(this).addClass("glow");
});
Here is the JSFiddle demo
Why do you need to do it like that? Can't you just set the class on the element clicked?
JavaScript
function index(el){
$(el).addClass("glow");
}
HTML
<div id="sticky" onclick='index(this)'></div>
Instead just pass this:
onclick='index(this)'
now in the function:
function index(el){
var e = $(el).attr('title');
$('#sticky[title="'+e+'"]').addClass("glow");
}
As the element itself is the target one then just use this:
function index(el){
$(el).addClass("glow");
}
or better to go unobtrusive, remove the inline event handler and use this way:
$('#sticky').on('click', function(e){
$(this).addClass("glow");
});
js at Question returns expected results. Missing closing > at html #sticky <div> tag at
onclick="index(this.title)"
following onclick attribute . Additionally,
+
should be removed at
+clicked_id
at .attr() setting title . javascript + operator attempting to convert clicked_id String to Number when placed before string
function index(clicked_id){
alert(clicked_id);
$('#sticky').attr("title", clicked_id).addClass("glow");
}
.glow {
color: purple;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<div id="sticky" class="" title="sticky1" onclick="index(this.title)">click</div>
<div class="abc">
<a><img></a>
<h4></h4>
<div class="xyz">
Hello
</div>
</div>
in above html code how do i check whether div having class abc has div having class xyz.
You can simply do this:
var list = document.querySelectorAll('div.abc div.xyz');
if (1 == list.length) {
alert("found");
}
Here's a demo: http://jsfiddle.net/3xQ5X/
Using JQuery:
$("div.abc").has("div.xyz");
Try something like this:
Get your parent div an id like abc.
var v = document.getElementById('abc');
for(var i in v.children)
{
if( v.children[i].nodeName == 'DIV')//this will tell if the parent div has children divs
{
console.log(v.children[i].className == 'xyz');//this will be true if the child div has a class named xyz.
}
}
Also remember to modify this script according to your requirement. I mean you can give an specific class in place of id to the divs you want to traverse. To select all the divs containing some specific class, use this link's function.
This script will do the needful.
<script type="text/javascript">
$(document).ready(function(){
if($("div.abc").children('div').hasClass("xyz"))
{
alert("found");
}
});
</script>
I have the following div structure
<div id="main_div">
<div id="lastChildDiv"></div>
</div>
now I want to create or append a new div above the div id of lastChildDiv. Then how to do it?
Use prepend
$('#main_div').prepend('<div id="new_div">...</div>');
Use before to append the newly created element:
$('#lastChildDiv').before('<div></div>');
You can use the .before()
$('#lastChildDiv').before('<div></div>');
var myNewDiv = '<div id='someId'></div>';
$('#main_div').html(myNewDiv);
or:
var myNewDiv = '<div id='someId'></div>';
$('#main_div').append(myNewDiv);