jQuery : How to operate a plugin on two input boxes - javascript

I'm writing a plug-in somewhat same like auto-complete or virtual keyboard with selected input values along with sorting and searching. This plugin is working fine to if I use it once in a page but when I apply this one to more then one then it is showing both the boxes and events are applied on the last written object. Sample code and fiddle is below:
http://jsfiddle.net/mantrig/ugmwa5b5/
my plug in code to in :
$(document).ready(function(){
var testData = ["BSL","DSK","NPNR","SAV","ET","NDLS","JPR","MAS","BCT","NZM","BR","SUJH"];
$(".stations").myAutoSuggest({
data:testData,
dataref:"stationsList",
title:"Station List",
sort:"desc"
});
var trainData = ["12345","32151","64231","56421","78542","13452"];
$(".trains").myAutoSuggest({
data:trainData,
title:"Train List",
dataref:"trainsList",
sort:"desc"
});
});
I'm very much new to writing any plug-in so code may be very dirty. A little help will be much appreciated!!

You missed '.
Change $(body).append(html); to $('body').append(html);
Jsfiddle

Problem is your "selectedTextbox" variable change here will solve your problem
$(".Help").bind("focus",function(e){
selectedTextbox=this;
showVirtualHelp(selectedTextbox);
});
and later using it here because there are multiple text boxes so you need to pass reference current focused element to show help box
function showVirtualHelp(selectedTextbox) {
$(".virtualHelp."+settings.dataref).css("top",($(selectedTextbox).offset().top+30)+"px");
$(".virtualHelp."+settings.dataref).css("left",($(selectedTextbox).offset().left+30)+"px");
$(".virtualHelp."+settings.dataref).show();
}
Working Fiddle

Related

.on('Input') dynamic updates and additions part 2

please see this question here for context...
The script has the function of adding various "clones" of the original inputs ID's for example :
#YXS
Would be come
#YXS2
This happens via this code from an external js file
// Increment input ID
clone.find(':input').attr('id', function(i, val) {
return val + count;
});
So in the original code :
$('#YXS, #YSM, #YMED, #YLRG, #YXLRG, #SM, #MED, #LRG, #XL, #XXL, #XXXL, #XXXXL').on("input", function() {
Ive tried adding "#YXS2" to the list, but its not working out as intended.
For more context here is the form here, but not 100% this is just example of its appearance. So you can see when you click the "+" icon to add a garment, a new instance of that form appears.
Hopefully my explanation isnt TOO sketchy! Any help is awesome help! Thanks in advance!

Grails rendered content accessing id elements dynamically

AJAX content is being rendered with a remoteLink function inside the form to populate a accordion (just a little background info).
The function attatchEmail(test) which is being called on the double-click of each paragraph content of the JQuery Accordion widget. This is what happens running the function... Screenshot of 1st alert & Screenshot of 2nd alert.
Is it not possible to select the paragraph and get the contents from the paragraph like below?
(I have tried changing .val to .html and .text. I have also tried $('#'+testingID))
_form.GSP
function attatchEmail(test) {
$(document).ready(function()
{
var testingID = test.id;
alert(testingID);
var testingValue = $(testingID).val();
alert(testingValue);
});
};
_contactListAjax.GSP
<g:each in="${contactList}" status = "i" var="contact">
<h3>${contact.contactSurname +', '+ contact.contactForename}</h3>
<div><p id="contact${contact.id}" ondblclick="attatchEmail(this)">${'Email: '+contact.email}</p></div>
</g:each>
Run out of avenues to explore, I'm sure I've done something simple like this before perfectly fine :/
See two screenshots please for better insight, thanks
Well, it looks like you're using jQuery but it also looks like you're not really buying into the jQuery methodology. Mixing behavior with markup is not really considered a good practice these days, especially when using a library like jQuery. I'm not sure of your exact issue but I would recommend changing it to something like the following:
<g:each in="${contactList}" status = "i" var="contact">
<h3>${contact.contactSurname +', '+ contact.contactForename}</h3>
<div><p class="contact-email" data-id="${contact.id}">${'Email: '+contact.email}</p></div>
</g:each>
$(function() {
$("body").on("dblclick", ".contact-email", attachEmail);
});
function attatchEmail(event) {
var $element = $(event.target);
var id = $element.data("id");
};
This should help fix any issues with dynamic rendering because of the way the jQuery on function works.

Removing Textboxes

Im new to js kindly help me! im using functionality to add text boxes dynamically im done with that part now i want make a js function that removes text boxes on a click on remove button. any help ?
You can use bellow JavaScript function to Remove elements.
function removeElement(divNum) {
var d = document.getElementById('myDiv');
var olddiv = document.getElementById(divNum);
d.removeChild(olddiv);
}
If you have more information you can visit here.
You can either remove a child with
document.getElementById(n).removeChild(thebox);
or you can do as on this thread Remove <p class="classname"> in Javascript? (not recommanded for your case)
Finally, you can use jquery (famous library) with the function $("#id_of_element").remove(); (best and easiest solution)

How to use variable to display certain content?

When I click the href, I want to display the image and description from the target id. Can someone correct this code for me and show me how to do it right? Thank you very much!
HTML:
<div id="gallery"><img/></div>
Click me
jQuery code:
var datas={
"images":[{ "src":"gallery/panorak.jpg",
"title":"PANORAK",
"date":"28/10/2010 Web Design",
"description":"desc",
"id":"panorak",
},
{
"src":"gallery/kamoa.jpg",
"title":"kamoa",
"date":"28/10/2010 Web Design",
"description":"desc",
"id":"kamoa",
},
]};
$(document).ready(function(){
var gallery=$("#gallery")
//****I don't know how to display it right at this point
var list=$("<ul/>");
gallery.append(list);
datas.images.each(function(i,j){
list.prepend($("<li/>").$("<img/>").attr("src",datas.images[i].src).load(function(){ }));
});
});
Any help is greatly appreciated! Thank you very much!
I have made some changes to your code:
http://jsfiddle.net/T5r6D/
Have a look at my jsfiddle link for all updates.
You had some structure in flow issues and javascript error.
I am adding all image in array using normal for loop. once loop is done, i wrap images with <li> and adding the list to the stage.
Keep in mind that images will not be shown coz it is not on the jsfiddle server. Try it on your side.
UPDATE
http://jsfiddle.net/T5r6D/1/
So the new update does not hide the full container.
The anchor #tag [href] will be matched with the images title tag. So once clicked on a button it will hide the image that matches.
Try this:
$(document).ready(function(){
var gallery=$("#gallery")
//****I don't know how to display it right at this point
$('a').click(function(){
var list=$("<ul/>");
gallery.append(list);
$.each(datas.images, function(i,j){
var img = $("<img/>").attr("src",datas.images[i].src);
list.prepend($("<li/>").append(img));
});
});
});
If you want to trigger events when something is clicked, you can use click() function. And I fixed each() function part. Check this documentation. And I split some code to improve readability.

Adding a dynamically created div to another div with jquery

I am creating two divs:
var div_day=document.createElement("div");
var div_dateValue=document.createElement("div");
I then want to add div_day to an existing calendar div and div_dateValue to div_day:
$('#calendar').append(div_day)
$(div_day).append(div_dateValue);
div_day gets added to calendar, but div_dateValue does not get added to div_day, and the script stops there. No errors in the console but it is in a loop and should have more div_days (each with a unique id). I am new to jquery so any help is appreciated.
In my search I have found how to add divs, but not to add a dynamically created div to another dynamically created div.
Thanks for your help!
Kevin
div_day.appendChild(div_dateValue)
$('#calendar').append(div_day)
Something else must be going on (even with your missing semi-colon). Your example works fine here:
http://jsfiddle.net/P4rh5/
But, instead of creating divs with straight javascript, you can do it with jQuery:
var div_day = $("<div>");
var div_dateValue = $("<div>");
$('#calendar').append(div_day);
$(div_day).append(div_dateValue);
Of course, you could do this in a single step:
$('#calendar').append("<div><div></div></div>");
$('<div><div></div></div>').appendTo("#calendar");
Try this and mark it as answer if it helps

Categories