3

Am very new to JavaScript / JQuery... Am trying to create a label which is similar to the e-mail addresses that one has in their TO: text field (as input) in Hotmail.

What I am seeking is a mechanism to pass in a String name and String e-mail into a JavaScript function which will produce a label (with the person's name) along with a pencil image (for edit during mouseover) and X (delete button).

How could I create this? Is there a JQuery plug-in that I can use to make it so a text field contains this dynamic label but the end user can click on on the X (and delete it) and / or click on the pencil image and see the e-mail address and edit it?

(1) Contact Labels:

Dynamic Contact Labels

(2) Edit Contacts:

enter image description here

Basically, I want to pass in a name and e-mail String into a JavaScript function and come up with these labels:

(1) function createDynamicLabel(String name, String email) {}

(2) function editDynamicLabel() {}

Would this require a div tag which holds two images (can these images be clicked on)? I can't seem to envision how to create these dynamic labels using HTML/CSS/JavaScript...

Any help would be most appreciative!

Thank you for taking the time to read this.

PacificNW_Lover
  • 4,746
  • 31
  • 90
  • 144

1 Answers1

0

I don't know any plugging based on jQuery for this.

If i would have to do this i would use some html element to group the email addresses and group then by class (viewMode and editMode).

You can make a $(emailSources).each() cicle to output the html code like this:

<div id="emailsContainer">
    <div id="johndoe@aol.com" style="min-width:10px; float:left;">
        <span></span>
        <img class="viewMode" src="pathToPencil" onclick="EmailChangeToEditMode("johndoe@aol.com");" />
        <img class="viewMode" src="pathToDelete" onclick="EmailDelete("johndoe@aol.com");" />
        <input type="text" class="editMode" />
        <img class="editMode" src="pathToOkButton" onclick="EmailSaveChanges("johndoe@aol.com");" />
    </div>
    <div id="jane@aol.com" style="min-width:10px; float:left;">
        <span></span>
        <img class="viewMode" src="pathToPencil" onclick="EmailChangeToEditMode("jane@aol.com");" />
        <img class="viewMode" src="pathToDelete" onclick="EmailDelete("jane@aol.com");" />
        <input type="text" class="editMode" />
        <img class="editMode" src="pathToOkButton" onclick="EmailSaveChanges("jane@aol.com");" />
    </div>
</div>

And those are the javascript functions:

function EmailChangeToEditMode(containerId) {
$(containerId).find(".viewMode").hide(); // this will hide all the elements of the ViewMode
$(containerId).find(".editMode").show(); // this will show all the elements of the EditMode
}

function EmailDelete(containerId) {
$(containerId).remove(); // removes from the DOM
}

function EmailSaveChanges(containerId) {
var newEmail = $(containerId).find("input[class='editMode'][type='text']").value; // gets the new Email Address typed from the user
$(containerId).find("span").text(newEmail) // sets the new email address that will be showed
$(containerId).find(".viewMode").show();
$(containerId).find(".editMode").hide();
}

Basicly you're using an container for the email address (the div). Then you have html elements for edit mode and view mode. The javascript code is using the hide(); and show(); to handle what you want to do.

You'll also have to hide all the editMode elements when you generate the html code (it starts with the viewMode elements). You'll have to set the text of the current email address on span too.

This code may not be working 100% because i wrote this without testing

Gui
  • 9,555
  • 10
  • 42
  • 54
  • Thank you so much for this, guilhermeGeek! Haven't tried it yet but when I do, I will let you know the results! With the exception of the Edit, one can use this library: http://harvesthq.github.com/chosen/ – PacificNW_Lover Aug 11 '11 at 22:39
  • Ok mate. After you test it and if this is what you were looking for, mark the reply as answered to close this topic. Cheers. – Gui Aug 24 '11 at 15:45
  • Thank you guilhermeGeek, I really appreciate it. Am wondering how I can incorporate your example into my code. My code is very similar to this (and is the present functionality): http://jsfiddle.net/bNDHy/ I use a Java Framework which creates all the user interface components (submit button, check boxes, text field). I do have access to JavaScript and HTML/CSS. So, I was wondering what the best approach is? Should I create a function which takes the values from the text field and populates it with dynamic labels using your source code? Would appreciate if you could look respond. Thanks a lot. – PacificNW_Lover Aug 31 '11 at 04:36