Solved

Javascript Open Wizard on click event from dynamic data

Posted on 2014-09-30
5
329 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 57

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 57

Expert Comment

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

Featured Post

Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
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…

729 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