how does stoppropagation works with Loops? - javascript

i create some Tiles in a foreach loop:
#foreach (var a in Model.AA)
{
<partial name="Partial/A/Tile" model="a" />
}
each of this Tiles have a Button to Start something. (Yes if the JS works i dont need the asp calls at the button)
my Problem is now: The js call works only at the first Tile but not at all others. But Why?
Button
<div class="Infos" Id=#Model.Id>
<a class="button" id="startsomething" url="#Url.Action(nameof(AController.Start), "A")" asp-action="Start" asp-route-Id="#Model.Id">Start me</a>
</div>
JS
$('#startsomething').click(function (event) {
var Id = $(this).parents('.Infos').attr('Id');
var url = $(this).attr('url');
event.preventDefault();
event.stopPropagation();
Start(url, Id)
});

ids are meant to be unique, so the event only registers to one id when you use #startsomething. Use a class name, and the selector .somethingClass.

Related

Deleting DOM elements with Javascript

I am currently having trouble figuring out exactly how to delete DOM elements.
I know that you can find the element you want by id and then delete it, the thing is in my case that i create the elements with a function each time the 'create' key is pressed. I also add a close button to each element to be able to delete it.
I know that you probably can find this online but i don't even know what to search for.
I want to add a click event to each button to be able to detect which one was pressed and then delete the corresponding element.
This is what I use to create the elements each time the button is pressed.
(I use the RE:DOM library to add the elements)
var count_id = 0;
function addChart(){
const test = el('.row',
el('.col',
el('.card mb-3',[
el('.card-header',[
el('i', {class: 'fa fa-area-chart'}),
el('a', {class: 'btn float-right', id: 'close-chart-'+count_id.toString()},
el('i', {class: 'fa fa-times'}))]),
el('.card-body',
el('#areaTest', {style:'width : 100%;'},
el('.loader'))),
el('.card-footer',
el('.row',
el('.col-lg-2',[
el('h6','Select a date'),
el('div', {class:'input-group date','data-provide':'datapicker'},[
el('.form-control', {type:'text'}),
el('.input-group-addon')])])
))])
));
test.id = count_id.toString();
mount(document.getElementById('charts-container'),test);
count_id++;
console.log(count_id);
}
Relevant HTML section. I am adding everything in this container.
<div class="row">
<div id="charts-container" class="container">
</div>
</div>
Create function delete and pass it id of element which is dynamicly assigned, you can see one example below, element has unique id "el-" and id assigned, that id is passed to function by button click and rest is done by remove() function
<button onclick="delete(123)"> DELETE </button>
<p id="el-123"> i will be deleted </p>
<script>
function delete(id){
document.getElementById("el-" + id).remove();
}
</script>
For your button to be clicked to delete the referred element(with parent i am assuming as corresponding complete row should be deleted), taking the structure as:
<div class="parentDiv">
<span>ele to be deleted</span>
<input class="delBtn" type ="button">Delete</input>
</div>
<script>
$(document).ready(function(){
$(".delBtn").click(function(event){
$(event.target).closest(".parentDiv").remove();
});
});
</script>
You can add click event on the element and pass its reference. And then you can call this function on its click event.
<div #divRef onclick="deleteElement(divRef)">I'm a div</div>
In script:
function deleteElement(element) {
element.parentNode.removeChild(element);
}

jQuery html onclick storing anchor ID, then pulling content from a javascript array

I'm trying to create a html anchor that has a unique ID and then when a user clicks the anchor, the ID gets passed to javascript via the onclick html tag and then a javascript script reads the ID and displays the content in a div. We're using jQuery library for this.
what I have so far:
<a id="MyID1" onclick="var ClickVariable=this.id;return false">1</a>
<a id="MyID2" onclick="var ClickVariable=this.id;return false">2</a>
<script>
var ClickVariable;
var ContentBox = [];
ContentBox[ClickVariable] = "Content for MyID1";
$(ClickVariable).click(function() {
$('.dropdown-menu-content').html(ContentBox);
});
</script>
The above does not work however we have an alternative that works but is not efficient.
<a id="MyID1">1</a>
<a id="MyID2">2</a>
$('#MyID1').click(function() {
$('.dropdown-menu-content').html('Text 1');
});
$('#MyID2').click(function() {
$('.dropdown-menu-content').html('Text 2');
});
As you can see the above one would work but is very repetitive for our needs because we have a large list to enter.
Here is a jsfiddle of the working one that is a tedious repetitive task:
http://jsfiddle.net/2z7o5hn3/
You can reuse the same handler like so:
//mapping id to string to display
var data = {
'MyID1': 'Text 1',
'MyID2': 'Text 2'
}
//shared click handler
var displayEl = $('.dropdown-menu-content');
function handler() {
displayEl.html(data[this.id]);
}
//add click handler to each id
$.each(data, function(k,v) {
$('#'+k).click(handler);
});
http://jsfiddle.net/2z7o5hn3/2/
Give all anchor tags a class and access it like this:
HTML:
<a id="ID1" class="clickVariables" href="#">ID1</a>
<a id="ID2" class="clickVariables" href="#">ID2</a>
JS:
$('.clickVariables').on('click', function(e){
e.preventDefault();
$('.dropdown-menu-content').html($(this).attr('id'));
})
do you mean like this? => DEMO
var texts=['Text 1','Text2'];
$('a[id^=MyID]').click(function() {
$('.dropdown-menu-content').html(texts[$(this).text()-1]);
});

Analysing button in html to check if can register for events

I want to register for events on a button in a web page using javascript addEventListener or something equivalent. But the web page doesn't appear to have standard form buttons. The html snippet below is the html markup for what appears as a button on the page.
I want to detect mousedown (or mouseclick or equiv). Is there any way I could detect the user clicking on this button?
<a href="javascript:" id="WIN_0_536870914" arid=536870914 artype="Control" ardbn="Dial" artcolor="null" class="btn btn3d arfid536870914 ardbnDial" style="top:247; left:115; width:46; height:21;z-index:1001;">
<div class="btntextdiv" style="top:0; left:0; width:46; height:21;">
<div class="f1" style=";width:46">Dial</div>
</div>
</a>
The only tricky bit will be getting the elements from the DOM in the first place. If you know the id then it's trivial to get this specific button:
var elem = document.getElementById('WIN_0_536870914');
elem.addEventListener('click', function () {
alert('click!');
});
http://jsfiddle.net/ugsYB/
Although you probably want to target all the buttons by their class:
var elems = document.getElementsByClassName('btn');
var i;
for(i = 0; i < elems.length; i++) {
var elem = elems[i];
elem.addEventListener('click', function () {
alert('click!');
});
}
http://jsfiddle.net/ugsYB/1/ (Note: there are cross browser issues with getElementByClassName)
Of course, jQuery makes this sort of thing trivial:
$('.btn').click(function () { alert('click!'); });
http://jsfiddle.net/ugsYB/2/
But it might be overkill for your needs.

Why do most of my jquery functions stop working after prepending a message to an existing list of messages?

HTML:
<div class='cf' id='content'>
<div id='leftColumn'>
</div>
<div class='microposts'>
<div class="micropostContent">
</div
</div>
<div id='rightColumn'>
</div>
</div>
Prepend:
$('.micropostContent').prepend('<%= j render("users/partials/micropost") %>');
Some of the functions:
$(".micropost_content").focus(function() {
this.rows = 7;
var micropostBox = $(this),
xButton = $(this).parent().find(".xButton");
micropostBox.hide().addClass("micropost_content_expanded").show().autoResize();
xButton.css("display", "block");
xButton.click(function() {
micropostBox.removeClass("micropost_content_expanded").addClass("micropost_content");
micropostBox.val("");
xButton.hide();
});
});
$(".comment_box").focus(function() {
this.rows = 7;
var $commentBox = $(this),
$form = $(this).parent(),
$cancelButton = $form.children(".commentButtons").children(".cancelButton");
$(this).removeClass("comment_box").addClass("comment_box_focused").autoResize();
$form.children(".commentButtons").addClass("displayButtons");
$form.children(".commentButtons").children(".cancelButton");
$cancelButton.click(function() {
$commentBox.removeClass("comment_box_focused").addClass("comment_box");
$form.children(".commentButtons").removeClass("displayButtons");
$commentBox.val("");
});
});
$('.micropostOptions').on('click',function(){
var postMenu = $(this).find('.postMenu');
if(postMenu.is(':hidden') ){
$('.postMenu').hide();
$('.micropostOptions').removeClass("postMenuHoverState");
postMenu.show();
$(this).hide().addClass('postMenuHoverState').show(60);
}else{
postMenu.hide();
$(this).removeClass("postMenuHoverState");
}
});
The functions that stop working are focus and click functions. They only work again after refreshing until I post another ajax message/micropost.
The .comment_box function doesn't work on the message that's been prepended but it works on all the previous messages. The .micropostOptions function doesn't work all. Then after refreshing everything works just fine.
Am I missing something?
Kind regards
Because the events are only bound to existing items when you declare them.. declare using on and attach to a parent element instead
$(document).on("focus", ".comment_box", function() {
//this will work with new elements
});
The closer the parent is to the element you are attaching functionality to the better, for example the parent container ".cf" or ".microposts" instead of document in your case.
To elaborate, in your example, you are only binding events to the individual elements, and when new items are added, they don't have the same event bindings... When you bind the event to a higher level element, then you are basically saying "apply this handler to everything inside of this parent element that matches this selector".

How to Reduce Size of This jQuery Script and Make it More Flexible?

I just created script that shows/hides (toggles) block of HTML. There are four buttons that each can toggle its HTML block. When any HTML block is opened, but user has been clicked on other button than that HTML block's associated button... it hides that HTML block and shows new one.
Here is what I have at the moment:
$('.btn_add_event').click( function() {
$('.block_link, .block_photos, .block_videos').hide();
$('.block_event').toggle();
});
$('.btn_add_link').click( function() {
$('.block_event, .block_photos, .block_videos').hide();
$('.block_link').toggle();
});
$('.btn_add_photos').click( function() {
$('.block_event, .block_link, .block_videos').hide();
$('.block_photos').toggle();
});
$('.btn_add_videos').click( function() {
$('.block_event, .block_link, .block_photos').hide();
$('.block_videos').toggle();
});
Any ideas how to reduce code size? Also, this script isn't very flexible. Imagine to add two new buttons and blocks.
like Sam said, I would use a class that all the blocks share, so you never have to alter that code. Secondly, you can try 'traversing' to the closest block, therefore avoiding it's name. That approach is better than hard coding each specific block, but if the html dom tree changes you will need to refactor. Last, but best, you can pass in the class name desired block as a variable to the function. Below is something you can copy paste that is close to what you started with.
$('.myAddButtonClass').click( function() {
$('.mySharedBlockClass').filter(':visible').hide();
//find a good way to 'traverse' to your desired block, or name it specifically for now.
//$(this).closest(".mySharedBlockClass").show() complete guess
$('.specificBlockClass').show();
});
I kept reading this "When any HTML block is opened, but user has been clicked on other button than that HTML block's associated button" thinking that my eyes were failing me when Its just bad English.
If you want to make it more dynamic, what you can do is add a common class keyword. Then
when the click event is raise. You can have it loop though all the classes that have the
keyword and have it hide them all (except the current one that was clicked) and then show the current one by using the 'this' keyword.
you can refer below link,
http://chandreshmaheshwari.wordpress.com/2011/05/24/show-hide-div-content-using-jquery/
call function showSlidingDiv() onclick event and pass your button class dynamically.
This may be useful.
Thanks.
try this
$('input[type=button]').click( function() {
$('div[class^=block]').hide(); // I resumed html block is div
$(this).toggle();
});
Unfortunatly I couldn't test it, but if I can remember right following should work:
function toogleFunc(clickObject, toogleTarget, hideTarget)
{
$(clickObject).click(function()
{
$(hideTarget).hide();
$(toogleTarget).toggle();
});
}
And the call:
toogleFunc(
".btn_add_videos",
".block_videos",
".block_event, .block_link, .block_photos"
);
and so far
Assuming the buttons will only have one class each, something like this ought to work.
var classNames = [ 'btn_add_event', 'block_link', 'block_photos', 'block_videos' ];
var all = '.' + classNames.join(', .'); // generate a jquery format string for selection
$(all).click( function() {
var j = classNames.length;
while(j--){
if( this.className === classNames[j] ){
var others = classNames.splice(j, 1); // should leave all classes but the one on this button
$('.' + others.join(', .')).hide();
$('.' + classNames[j]).toggle();
}
}
}
All the buttons have the same handler. When the handler fires, it checks the sender for one of the classes in the list. If a class is found, it generates a jquery selection string from the remaining classes and hides them, and toggles the one found. You may have to do some checking to make sure the strings are generating correctly.
It depends by how your HTML is structured.
Supposing you've something like this
<div class="area">
<div class="one"></div>
<div class="two"></div>
<div class="three"></div>
</div>
...
<div class="sender">
<a class="one"></a>
<a class="two"></a>
<a class="three"></a>
</div>
You have a class shared by the sender and the target.
Your js would be like this:
$('.sender > a').click(function() {
var target = $(this).attr('class');
$('.area > .' + target).show().siblings().hide();
});
You show your real target and hide its siblings, which aren't needed.
If you put the class postfixes in an array, you can easily make this code more dynamic. This code assumed that it doesn't matter in which order toggle or hide are called. If it does matter, you can just remember the right classname inside the (inner) loop, and toggle that class after the loop.
The advantage to this approach is that you can extend the array with an exta class without needing to modifying the rest of the code.
var classes = new Array('videos', 'event', 'link', 'photos');
for (var i = 0; i < classes.length; ++i)
{
$('.btn_add_' + classes[i]).click(
function()
{
for (var j = 0; j < classes.length; ++j)
{
if (this.hasClass('btn_add_' + classes[j]))
{
$('.block_' + classes[j]).toggle();
}
else
{
$('.block_' + classes[j]).hide();
}
}
});
}
You could make this code more elegant by not assigning those elements classes like btn_add_event, but give them two classes: btn_add and event, or even resort to giving them id's. My solution is based on your description of your current html.
Here is what I think is a nice flexible and performant function. It assumes you can contain your links and html blocks in a parent, but otherwise it uses closures to precalculate the elements involved, so a click is super-fast.
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js" ></script>
<script type="text/javascript">
// Enables show/hide functionality on click.
// The elements within 'container' matching the selector 'blocks' are hidden
// When elements within 'container' matching the selector 'clicker' are clicked
// their attribute with the name 'clickerAttr' is appended to the selector
// 'subject' to identify a target, usually one of the 'blocks'. All blocks
// except the target are hidden. The target is shown.
//
// Change clickerAttr from 'linkTarget' to 'id' if you want XHTML compliance
//
// container: grouping of related elements for which to enable this functionality
// clicker: selector to element type that when clicked triggers the show/hide functionality
// clickerAttr: name of the DOM attribute that will be used to adapt the 'subject' selector
// blocks: selector to the html blocks that will be shown or hidden when the clicker is clicked
// subject: root of the selector to be used to identify the one html block to be shown
//
function initToggle(container,clicker,clickerAttr,blocks,subject) {
$(container).each(
function(idx,instance) {
var containerElement = $(instance);
var containedBlocks = containerElement.find(blocks);
containerElement.find(clicker).each(function(idxC, instanceClicker) {
var tgtE = containerElement.find(subject+instanceClicker.getAttribute(clickerAttr));
var clickerBlocks = containedBlocks.not(tgtE);
$(instanceClicker).click(function(event) {
clickerBlocks.hide();
tgtE.toggle();
});
});
// initially cleared
containedBlocks.hide();
}
);
}
$(function() {
initToggle('.toggle','a.link','linkTarget','div.block','div.');
});
</script>
</head>
<body>
Example HTML block toggle:
<div class="toggle">
a <br />
b <br />
c <br />
<div class="A block"> A </div>
<div class="B block"> B </div>
<div class="C block"> C </div>
</div> <!-- toggle -->
This next one is not enabled, to show scoping.
<div class="toggle2">
a <br />
<div class="A block">A</div>
</div> <!-- toggle2 -->
This next one is enabled, to show use in multiple positions on a page, such as in a portlet library.
<div class="toggle">
a <br />
<div class="A block">A</div>
</div> <!-- toggle (2) -->
</body>
</html>

Categories