Solved

Javascript Open Wizard on click event from dynamic data

Posted on 2014-09-30
5
323 Views
Last Modified: 2014-09-30
With some C# code, I'm building a Table including a <DIV> tag that I've added a value to that refers to an object ID.

The table will have multiple rows, each having a different object ID, and its own unique <DIV> tag, ie:

<table>
  <tr>
  <th>Function</th>
  <th>Name</th>
  <th>ID</th>
  </tr>
  <tr>
    <td><div id='edit' value='1234'>Edit</div></td>
    <td>Object One</td>
    <td>1234</td>
  </tr>
  <tr>
    <td><div id='edit' value='1235'>Edit</div></td>
    <td>Object two</td>
    <td>1235</td>
  </tr>

Open in new window


I want to be able to use Jquery or Javascript to retrieve the value of which DIV was clicked...  How would I do that?

    var wizard = $("#campaignWizard").wizard();
    $("#edit").click(function () {
        var clickedID = {????};
        wizard.show();
    });

Open in new window

0
Comment
Question by:adworldmedia
  • 3
  • 2
5 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 40352092
ID attribute MUST be unique in a document.
In you case use a class instead.

Test page : http://jsfiddle.net/bkjb1u9t/1/

For teh sake of your question, use :
var wizard = $("#campaignWizard").wizard();
    $("div[id='edit']").click(function () { //  if you change it to a class use : $("div.edit")
        var myCell = $(this).closest("td"); 
        var nextTD1 = myCell.next();
        var nextTD2 = nextTD1.next();
        alert( nextTD1.text() );
        alert( nextTD2.text() );
        wizard.show();
    });

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
ID: 40352094
0
 
LVL 55

Accepted Solution

by:
Julian Hansen earned 500 total points
ID: 40352162
I would do it like this.

Output your html like shown below - instead of attribute value use the html5 standard for adding custom attributes which prefix your custom values using the data prefix
HTML
<table>
  <tr>
  <th>Function</th>
  <th>Name</th>
  <th>ID</th>
  </tr>
  <tr>
    <td><div class='edit' data-d='1234'>Edit</div></td>
    <td>Object One</td>
    <td>1234</td>
  </tr>
  <tr>
    <td><div class='edit' data-id='1235'>Edit</div></td>
    <td>Object two</td>
    <td>1235</td>
  </tr>

Open in new window

Now the javascript is trivial
    var wizard = $("#campaignWizard").wizard();
    $('div.edit').click(function() {
       // HERE WE GET THE ID VALUE IN THE DIV'S CUSTOM DATA ATTRIBUTE data-id
       // USING THE JQUER .dat() function

       var  clickedID  = $(this).data('id');
        wizard.show();
    });

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
ID: 40352168
from julianH code, you should read, line 8, data-id instead data-d
0
 
LVL 55

Expert Comment

by:Julian Hansen
ID: 40353076
@leakim - thanks for catching that
0

Featured Post

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question