unable to remove a dynamically created Div - javascript

I am dynamically creating a div using jquery named "sampageswrapper", the thing is that I want to check if the div name sampageswrapper is already created then delete it.
<div id="container2" class="container" style="width:600px; height: 700px" name="container2">
Enter Color Name and use + to add more colors:
<input id="byname" type="text" name="byname">
<br>
<br>
<div class="sampageswrapper">
<div class="pagenumbering" align="center" style="clear: both; margin-top:12px; color:#FFF">
<div class="buttons">
</div>
<div class="pagecount" align="center">
</div>
<div class="sampageswrapper">
</div>
And following is my JS/Jquery Code
if ($("#sampageswrapper").length > 0) {
jQuery('#container2').find('#sampageswrapper').remove();
//i also tried jQuery('#sampageswrapper').remove();
}
But I am unable to remove the div please guide me.

You are selecting sampageswrapper based on ID, but you should test on the class.
jQuery('#container2').find('.sampageswrapper').remove();
jQuery('#container2 .sampageswrapper').remove(); // works too

Seem like you have duplicated id, since id must be unique you can use class instead for your dynamically added elements, then you can do:
if ($("#container2 .sampageswrapper").length > 0) {
jQuery('#container2').find('.sampageswrapper').remove();
}

You try to remove an object with id sampageswrapper, but in the html that is the class names. Use .sampageswrapper instead in your remove statement!

Add a breakpoint:
if ($("#sampageswrapper").length > 0) {
debugger;
jQuery('#container2').find('#sampageswrapper').remove();
}
Open console and refresh page, once it stops on the breakpoint, in console run this:
jQuery('#container2')
Make sure it returns the object you expect. Then run this in console:
jQuery('#container2').find('#sampageswrapper')
Here you'll see it returns nothing. This is because the '#sampageswrapper' selector is wrong and should be a class selector instead:
jQuery('#container2').find('.sampageswrapper')
You should be going through this process whenever you have a problem with code that involves jquery selectors, so that you can focus in on the specific selector that is causing the problem.

if ($(".sampageswrapper").length > 0) {
jQuery('#container2').find('.sampageswrapper').remove();
}
use class selector instead of id selector

Related

Jquery : How to access div tag under script in?

I have the below html. I am trying to access the div tag warningMessage under scripts.
<script id="notePopup" type="text/x-kendo-template">
<p class="notePopupMessage">Please enter a note:</p>
<input class="textNote k-textbox" type="text" maxlength="512" />
<div class="buttons">
<button class="add k-button">Add</button>
<button class="cancel k-button">Cancel</button>
</div>
<div id = "warningMessage" style="visibility: hidden" >
<p id="warningMsg" > Status change action note would not be saved until Save button is selected. </p>
<div>
</script>
I tried to access div id warningMessage and set the visibility to not hidden on certain events by doing something like this$("#warningMessage").show(); and it did not work.
What do we have to do different in order to access this div tag. I suspect its behaving like this because it is under a script tag.
Here is the Fiddle link.
Try running the kendo script first, then access the element by running the jQuery function you mentioned? I guess when the kendo script is finished, that template will be converted to valid html, and your div will be accesible for jQuery.
It won't work because you first set a visibility:hidden.
So either you set it to visible when the event occurs, or you first display:none this div and make it display:block (using show(), fadeIn() and so on) for this specific event:
Here is a fiddle for you: https://jsfiddle.net/liototo/jnmyswy4/1/
I set the warning div to:
#warningMessage{
display: none;
}
The jQuery part is then as follow (just an example):
$('.add').on('click' , function(){
if ($('.textNote').val())
$('#warningMessage').show();
else
alert('enter something!');
});

Write click event dynamically on jquery

Here is the original html
<div class='cl_{{ $gen }}'>{{$gen->name}}</div>
<div class='jl_{{ $var }}'>{{$gen->name}}</div>
After looping over here is the html i got as output
I wanted to do if i click on the class cl_x, then the jl_x should be visible and other should be hidden and by default the first cl_1 should be visible. How can i do this ?
<div class='cl_1'>One</div>
<div class='cl_5'>Two</div>
<div class='cl_6'>Three</div>
<br>
<div class='jl_1'>Alpha</div>
<div class='jl_1'>Andrew</div>
<div class='jl_1'>Christ</div>
<div class='jl_5'>Anto</div>
<div class='jl_5'>Brito</div>
<div class='jl_6'>Oyster</div>
<div class='jl_6'>Beta</div>
Note : All the 1,5,6 are not standard as they are coming from database.
I really can't able to think how to achieve this. Help pls
But Here is what i have tried the logic
Inside Document Ready
Loop over the jquery like html
Write click event to show hide if they cick on cl_*
Trigger the click event for first occurance.
Don't worry about the html generated but the need is to write the jquery events dynamically or something else
But can't able to implement the code pls help
Script :
$(document).ready(function() {
//not sure whether i should loop over the jquery itself or write anything like the element starts with cl-* like that
});
Update :
Here is the Fiddle i have so far
You don't really want to use class for this - a custom data attribute makes sense, though. Like <div class="cl" data-number="{{ $gen }}"> with <div class="jl" data-number="{{$var}}"> on the other elements.
Then inside the $(document).ready(...) you can do something like:
$('.jl').hide().first().show();
$('.cl').click(function(){
$('.jl').hide().filter('[data-number="' + $(this).data('number') + '"]').show();
})
It would also be good to make up more meaningful names than "cl" and "jl" - classes should generally be semantic.
Would move the unique identifiers to a different attribute and add a common class to all the J group
<div class='cl' data-gen="{{ $gen }}">{{$gen->name}}</div>
<div class='jl jl_{{ $var }}'>{{$gen->name}}</div>
JS
$(document).on('click','.cl',function(){
$('.jl').hide().filter('.jl_' + $(this).data('gen') ).show();
})
Add a base "jl" class to all your html that has anything with a "jl_*" so that you will have access to anything overall that has the "jl" class then toggle it hidden or not hidden like so:
HTML:
<div class='cl {{ $gen }}'>{{$gen->name}}</div>
<div class='jl {{ $var }}'>{{$gen->name}}</div>
JS:
$(document).on('click', '.cl', function(e){
var classes = ($(e.target).attr("class").split(' '));
$('.jl').toggleClass("hide");
$('.jl' + "."+ classes[1]).toggleClass("hide");
});
Style:
.hide{
display: none;
}
https://jsfiddle.net/rc368gjp/

Changing span content with javascript

I'm quite new with jasascript and jQuery and I'm trying to create a pop-up that will appear when a user have reach an achivement. This pop-up will have the user name and the tittle of the achivement.
Here's the HTML
<div class="popUp">
<img src="" alt="avatar" id="imgPop"/>
<h3 class="nomUser"><span></span></h3>
<p class="popAtteint">A atteint l'objectif:</p>
<p class="objAtteint"><span></span></p>
</div>
and this is the javascript
function afficher(nom,objectif){
if(!$(".nomUser").length){
$(".nomUser").append("<span></span>");
}
if(!$(".objAtteint").length){
$(".objAtteint").append("<span></span>");
}
$(".nomUser span").append(nom);
$(".objAtteint span").append(objectif);
$(".popUp").animate({opacity:100},1000);
$(".popUp").delay(1000).animate({opacity:0},2000,function(){
$(".nomUser span").remove();
$(".objAtteint span").remove();
});
}
the first time it pop up it works perfecly but it doesn't any other time... I think the problem is that my "if" aren't working and i can't figure out why.
(sorry for my english)
Your two if statements checking the length aren't checking for the presence of the right element, which is causing your selector that's trying to append to not find anything.
The first time through your dom looks like:
<div class="nomUser">
<span></span>
</div>
but the second time through, it would look like:
<div class="nomUser">
</div>
When the code to append 'nom' is run, it doesn't execute because it can't find a span nested under an element with the nomUser class
$(".nomUser span").append(nom); // Not found :-(
The following should work:
function afficher(nom,objectif){
if(!$(".nomUser span").length){
$(".nomUser").append("<span></span>");
}
if(!$(".objAtteint span").length){
$(".objAtteint").append("<span></span>");
}
$(".nomUser span").append(nom);
$(".objAtteint span").append(objectif);
$(".popUp").animate({opacity:100},1000);
$(".popUp").delay(1000).animate({opacity:0},2000,function(){
$(".nomUser span").remove();
$(".objAtteint span").remove();
});
}
!$(".nomUser").length
This means there is not such element. Use
if ($(".nomUser").text().length == 0)
If you want to check it on empty

show element not working

Hi I have a problem with my function, when I call it to show my hidden div it does not work. It does not show the hidden div. I followed previous examples from what have been posted in stackoverflow but still my code does not work.
This is my html file
<div id="playTheGame" class="css/outer" style="display:none;" >
<div class="css/inner">
<h1>Choose!</h1>
<section id="hand">
<img src="images/rock.png">
<img src="images/paper.png">
<img src="images/scissors.png">
</section>
</div>
</div>
My Function
<script>
function logSuccess(){
document.getElementById("playTheGame").style.visibility="visible";
}
</script>
The Button I used for the function
<input type="button" onclick="logSuccess()" value="Show">
Change your code to this
document.getElementById("playTheGame").style.display = "block";
Since you hid it using the display property, show it using the display property.
There are two options for this:
One using JavaScript:
object.style.display="block"; // where object will be the playThemGame id element..
And the other one using jQuery; JavaScript library.
$("#playTheGame").show();
The option two won't work, because you will have to write the event function too, So just use the first one as:
document.getElementById("playTheGame").style.display="block";
Edit:
Since you are using
document.getElementById("playTheGame").style.display="block";
To disply the result, then you must use this to remove the display!
document.getElementById("playTheGame").style.display = "none"; to hide it back!
The basic idea is that, this will just shift the object-->style-->display's value to none Its not going to add any more attribute. Its just going to shift current attribute's value.

JQuery. Remove a previous sibling in the DOM tree

I have the next code dynamically created using JQuery. Theere are multiple row class divs placed one under the other.
<div class="row">
....
</div>
<div class="row">
<div class="line_type"></div>
<div class="download_value"></div>
<div class="flag"></div>
<div class="email"></div>
<div class="prize"></div>
</div>
<div class="row">
....
</div>
After i create these divs I have a "pointer" to a specific div which is of class row. In JQuery, how do i make it so I go down the DOM tree, until i reach the div of class line_type and download_value and remove them both, and also I'd like to go one more node down, at the div of type email and change some of it's CSS attributes.
I was not able to find anything on the web, maybe it's cause i'm a noob at these still.
I have a "pointer" to a specific div which is of class row ->
Assuming that you have the this object of the corresponding div with class row.. then you can use .find to get the line_type and download_value inside that div.
$(this).find('.line_type').remove();
$(this).find('.download_value').remove();
Then you can use the same .find to get the div with class email and access the .css
$(this).find('.email').css(/* You code*/);
Assuming row_pointer points to the row in question:
$('.line_type, .download_value', row_pointer).remove();
$('.email', row_pointer).css(...);
check this out
$('div.row').bind('click', function() {
$this = $(this);
$('div.line_type, div.download_value', $this).remove();
$('div.email', $this).css('background-color', 'red');
});
http://jsfiddle.net/YvyE3/

Categories

Resources