Getting index of a clicked element - dom

Hi I have this code. I need to get the 0 based index of the clicked element.
This is my code. I always get -1 as the index.
Ideally clicking first link would print 0 and second would print 1.
I am using jquery 1.3.2. JavaScript code is fine too.
What I am missing?
<script type="text/javascript">
function handleClick(id) {
var par = document.getElementById('par'+id);
alert('Index= ' + $('#clips').index(par));
}
</script>
<div id="clips" style="clear:both">
<div id="par30" class="alb rc32">
<div class="fl rc32 thm">
<a id="30" onclick="handleClick(30);">Link 1</a>
</div>
</div>
<div id="par40" class="alb rc32">
<div class="fl rc32 thm">
<a id="40" onclick="handleClick(40);">Link 2</a>
</div>
</div>
<div id="par50" class="alb rc32">
<div class="fl rc32 thm">
<a id="50" onclick="handleClick(50);">Link 3</a>
</div>
</div>
</div>

This works:
<script type="text/javascript">
function handleClick(id) {
var $par = $('#par' + id);
alert($par.index());
}
<div id="clips" style="clear:both">
<div id="par30" class="alb rc32">
<div class="fl rc32 thm">
Link 1
</div>
</div>
<div id="par40" class="alb rc32">
<div class="fl rc32 thm">
Link 2
</div>
</div>
<div id="par50" class="alb rc32">
<div class="fl rc32 thm">
Link 3
</div>
</div>
</div>

You are using the index function wrong.
Try
<script type="text/javascript">
function handleClick(id) {
var par = document.getElementById('par'+id);
alert('Index= ' + $('#clips > div').index(par));
}
</script>
see http://api.jquery.com/index/
If .index() is called on a collection
of elements and a DOM element or
jQuery object is passed in, .index()
returns an integer indicating the
position of the passed element
relative to the original collection.

Related

Button behaving globally and not for each item in Salesforce LWC

I am quite new to Lighting Web Component. However, I have some experience with other component base frameworks.
I'm creating a button for each item and when the button is clicked for each item. It applies to all items. In other words, when one clicks 'Extend' it is meant to show a date input for that specific item but it shows too all of them. This is how my code looks like:
<template>
<template if:true={subscriptionData}>
<template for:each={subscriptionData} for:item="subscriptionItem">
<div key={subscriptionItem.Id}>
<div>
<div class="slds-grid slds-gutters slds-grid_vertical-align-center">
<div class="slds-col custom-card-title-container">
<h2 class="custom-title">{subscriptionItem.SBQQ__ProductName__c}</h2>
</div>
<div class="slds-col button-custom">
<lightning-button label="Extend" title="Non-primary action" onclick={handleShowEditEndDate} class="slds-m-right_none custom-margin"></lightning-button>
</div>
</div>
<div class="slds-grid slds-m-top_xx-small">
<div class="slds-col">
<div class="slds-grid slds-grid_vertical">
<div class="slds-col slds-m-bottom_xx-small">
<span>Start Date</span>
</div>
<div class="slds-col">
<span>{subscriptionItem.SBQQ__StartDate__c}</span>
</div>
</div>
</div>
<div class="slds-col">
<div class="slds-grid slds-grid_vertical">
<div class="slds-col slds-m-bottom_xx-small">
<span>End Date</span>
</div>
<div class="slds-col">
<span>{subscriptionItem.SBQQ__EndDate__c}</span>
</div>
</div>
</div>
<div class="slds-col">
<div class="slds-grid slds-grid_vertical">
<div class="slds-col slds-m-bottom_xx-small">
<span>Quantity</span>
</div>
<div class="slds-col">
<span>{subscriptionItem.SBQQ__Quantity__c}</span>
</div>
</div>
</div>
</div>
<template if:true={showEditField}>
<lightning-layout multiple-rows class="slds-m-top_xx-small">
<lightning-layout-item size="8">
<lightning-input
type="date"
name="endDateInput"
label="End Date"
variant="label-inline"
>
</lightning-input>
</lightning-layout-item>
</lightning-layout>
</template>
<hr class = "custom-line">
</div>
</div>
</template>
</template>
export default class ExtendCPQSubscriptionLWC extends LightningElement {
#api recordId;
#track subscriptionData;
#track columns = columns;
showEditField = false
#wire(getSubscriptionsLWC, { recordId: '$recordId'})
loadFoundSubscriptions({error, data}) {
if (data) {
console.log('OK');
console.log(data);
this.subscriptionData = data;
} else if (error) {
console.log('ERR ' + JSON.stringify(error));
}
}
I would appreciate any input thanks in advance.
You have common showEditField used to control visibility/addition of date input. My recommendation is to move that information inside subscriptionData and update the track variable. This would trigger re-render of UI and only show that field for item, for which Extend button was clicked

displaying same div on click side by side and limit it to screen width with latest click first

i am new in web development. i have data(username) appearing on website. Now, i want to make popups, chat boxes just like facebook. each username has different div id. I already have a div for chat box(msg_box). I need this div to show up side by side on each click at bottom of page . also latest clicked username appear first. thanks.
<script>
$(document).ready(function(){
$(".nameid").click(function() {
var text = $(this).text();
document.getElementById("bb").innerHTML=text;
});
});
</script>
/head>
<body>
<div class="container">
<div class="header">
</div>
<div class="leftblock">
</div>
<div class="chat_box" id="cb">
<div class="chat_head_left"></div>
<div class="chat_head_right"></div>
<div class="chat_head">Members</div>
<div class="chat_body">
<?php
for($i=1;$usernamee=mysqli_fetch_assoc($records); $i++)
{
echo "<div class='nameid' id='$i' <b>".$usernamee['username']."
</br>"."</b></div>";
}
?>
</div>
</div>
<div class="msg_box" style="right:290px">
<div id="bb" class="msg_head">
<div class="close">x</div>
</div>
<div class="msg_wrap">
<div class="msg_body"></div>
<div class="msg_footer" ><textarea class="msg_input"
rows="4">sample</textarea></div>
</div>
</div>

jquery on show and on hide not working with class selector

I am trying to enter certain roles in a text area and have it highlighted in a popup block using bootstrap.
The expected behavior is that the selected roles must be highlighted, the code works when I use the id selector to the jquery, but the same code does not work with class selector
<textarea name="options" id="options" cols="30" rows="10">admin user</textarea>
<a class="btn" data-toggle="modal" href="#myModal">Launch Modal</a>
<div class="modal hide fade" id="myModal" class="myModal">
<div class="modal-header">
<a class="close" data-dismiss="modal">×</a>
<h3>Modal header</h3>
</div>
<div class="modal-body">
<div>
<ul>
<li class="mlist">admin</li>
<li class="mlist">user</li>
<li class="mlist">guest</li>
<li class="mlist">blank</li>
</ul>
</div>
</div>
<div class="modal-footer">
Close
</div>
</div>
<script type="text/javascript">
$(document).ready(function (){
console.log('Inside onready');
$('.myModal').on('show', function () {
console.log('Inside show');
var option = $('#options').val();
var options = option.split(" ");
console.log('Selected roles: ' + options);
for (var index = 0; index < options.length; index++) {
highlight(options[index]);
};
});
$('.myModal').on('hide', function () {
console.log('Inside onhide');
$('.mlist').removeClass('btn-primary');
});
});
function highlight(parOptions) {
$('.mlist:contains("' + parOptions +'")').addClass('btn-primary');
}
</script>
I guess it's something to do with your div having two class tags.
<div class="modal hide fade" id="myModal" class="myModal">
Change to
<div class="modal hide fade myModal" id="myModal">

Simple 2 line jQuery Accordion menu...Need help !

This is my accordion menu :
<script type="text/javascript">
$(function()
{
$("dt.title").click(function()
{ $("div.info").slideUp("slow"); $(this).next("div.info:hidden").slideDown("normal" );
});
});
</script>
It is working fine, here have a look at the html:
<body>
<div id="wrapper">
<div id="container">
<ul id="nav">
<dt class="title">About</dt>
<dt class="title">Work</dt>
<div class="info">
<ul>
<li>Bol</li>
<li>Annie</li>
<li>Imran</li>
</ul>
</div>
<dt class="title">Contact</dt>
<dt class="title">Links</dt>
</ul>
</div>
<div id="content">
</div>
</div>
</body>
</html>
Very simple ! But when I put the href links, the menu stop working. For example:
<dt class="title">About</dt>
Please help me with this, I'll be really thankful !
Cheers,

wrapping elements in a new div

I have the below html:
<div id="one">
<div class="two">
<div class="entry">
Content here
</div>
<div class="entry">
Content here
</div>
<div class="entry">
Content here
</div>
<div class="entry">
Content here
</div>
<div class="entry">
Content here
</div>
<div class="entry">
Content here
</div>
<div class="entry">
Content here
</div>
<div class="entry">
Content here
</div>
<div class="entry">
Content here
</div>
</div>
</div>
in jQuery if I am to wrap every 3 elemnts in a new div like below how could I do that?
<div id="one">
<div class="two">
<div>
<div class="entry">
Content here
</div>
<div class="entry">
Content here
</div>
<div class="entry">
Content here
</div>
</div>
</div>
<div class="entry">
Content here
</div>
<div class="entry">
Content here
</div>
<div class="entry">
Content here
</div>
</div>
<div>
<div class="entry">
Content here
</div>
<div class="entry">
Content here
</div>
<div class="entry">
Content here
</div>
</div>
</div>
</div>
I tried slice and wrap which did not really help me. Any ideas? appreciate your help on this.
Thanks,
L
var entries = $('#one > .two > div.entry');
entries.each(function(i) {
if( i % 3 == 0 ) entries.slice(i,i+3).wrapAll('<div>');
});
http://jsfiddle.net/PZkSL/
Refer to this How to wrap every 3 child divs with html using jquery? or Wrap every 3 divs in a div
How about getting the div element with class "two", and iterate through it's children. When you find the first child use $(firstChild).before('<div>'); and on the third child use $(thirdChild).after('</div>'); Just iterate all the way through using this method. Haven't used .before() or .after() so not sure how this will pan out. Good luck.
I would just write a simple for loop, something like:
var two = $(".two");
var elements = $(".entry");
var group = [];
for(var i=0;i<elements.length;i++) {
var mod = i % 3;
if(mod == 0 && group.length > 0) {
var container = $('<div></div>');
two.append(container);
for(var j=0;j<group.length;j++) {
container[0].appendChild(group[j]);
}
group = [];
}
group[mod] = elements[i];
}