1

In my form there are 4 links when they clicked content of my div tag is shown using jQuery.

My question is, when link is clicked I want to pass name of that link to that div tag, how is it possible?

            $("#adduser").click(function() {
              $("#add").toggle("slow");
            });
<div class="box-content" id="adduser">
  <a class="quick-button span2" href="#">
    <i class="icon-group"></i>
    <p>Add Mentors</p>
    <span class="notification green">167</span>
  </a>
  <a class="quick-button span2" href="#">
    <i class="icon-group"></i>
    <p>Add Students</p>
  </a>
  <a class="quick-button span2" href="#">
    <i class="icon-group"></i>
    <p>Add Faculties</p>
  </a>
  <a class="quick-button span2" href="#">
    <i class="icon-group"></i>
    <p>Add Faculty Advisors</p>
  </a>
</div>


<div class="row-fluid sortable " id="add" style="display: none">
  <div class="box span12">
    <div class="box-header" data-original-title>

      <h2><i class="halflings-icon white edit"></i><span class="break"></span>Add new User</h2>
      <div class="box-icon">
        <a href="#" class="btn-minimize"><i class="halflings-icon white chevron-up"></i></a>
        <a href="#" class="btn-close"><i class="halflings-icon white remove"></i></a>
      </div>
    </div>
    <div class="box-content">
      <form enctype='multipart/form-data' method="post" class="form-horizontal" action="">
        <div class="control-group">
          <label class="control-label" for="fileInput">Select file</label>
          <div class="controls">
            <input class="input-file uniform_on" id="file" name="file" type="file">
            <input type="submit" class="blue btn" value="Add">

          </div>
          <br>
          <div class="controls">
            <a href="">Or add single user</a>
          </div>
        </div>

      </form>
    </div>
  </div>
</div>
JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Sandhya Gor
  • 1,400
  • 3
  • 9
  • 19

2 Answers2

0

I am not sure if you are looking for this . Can you pls check ..

$("a").click(function(){
     // Get Link Name Attribute
    var LinkName=$(this).attr("name");
    //Get Link Text/Value
    var LinkText=$(this).text();
    
    //Display for demo
    alert(LinkName+'\n'+LinkText);
  
    //Display LinkText in SomeDiv
    $('#SomeDiv').text(LinkText);
    
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="#" name="Anchor Name">Link Text</a><hr/>

<div id="SomeDiv"></div>
NewToJS
  • 2,762
  • 3
  • 14
  • 22
srinath
  • 2,748
  • 6
  • 35
  • 56
  • pls run it with jquery library .. it should give you the name of that link – srinath Mar 02 '15 at 07:24
  • Your JsFiddle isn't working, it would be a good idea to display the full source code/answer in your post. – NewToJS Mar 02 '15 at 07:26
  • Did you select jquery 1.9.1 from the drop down . its working for me – srinath Mar 02 '15 at 07:27
  • You should have this pre-selected since this is supposed to be an example of the answer. – NewToJS Mar 02 '15 at 07:28
  • yes its working and its fine.. but i just dont want to display it. i wants to store that value in one variable and wants to access it in my div portion. is it possible?? – Sandhya Gor Mar 02 '15 at 07:29
  • am newbie in jsfiddle. this is my first exampe. i am trying to update it , but it is not reflecting. thats why i specifically gave a comment to run it with jquery library. – srinath Mar 02 '15 at 07:30
  • @user2302126 yes.. you can just create your variable with any name and store the value. var link_name = $(this).attr("name"); And you need to specify what you exactly need to do in that div portion . that would help others to answer. You can play around with this variable. – srinath Mar 02 '15 at 07:31
  • Since the OP doesn't have the name attribute on the anchor tags you might want to swap `.attr("name")` for `.text()` this is will get the value/text of the anchor tag. – NewToJS Mar 02 '15 at 07:32
  • ok fine. in a
    portion there is a heading like add a new user instead of user i wants to display add a new mentor or add a new student as per the link clicked
    – Sandhya Gor Mar 02 '15 at 07:35
  • @srinath I have made some changes to your answer, please check to accept/decline. – NewToJS Mar 02 '15 at 07:48
  • `Jenish Rabadiya ` has rejected voted to reject my edit. I'm assuming Jenish didn't look at the JS Fiddle before rejecting since i have move your example from JS fiddle to your post and added the option of using the anchor text rather than name attribute. – NewToJS Mar 02 '15 at 07:52
0

I've just changed the header to print out the pressed link name.

hope that's help

$(function() {
 $(".quick-button").click(function(event) {
  $("h2").text($(event.target).text());  
  $("#add").toggle("slow");
 }); 
});
<div class="box-content" id="adduser">
  <a class="quick-button span2" href="#">
    <i class="icon-group"></i>
    <p>Add Mentors</p>
    <span class="notification green">167</span>
  </a>
  <a class="quick-button span2" href="#">
    <i class="icon-group"></i>
    <p>Add Students</p>
  </a>
  <a class="quick-button span2" href="#">
    <i class="icon-group"></i>
    <p>Add Faculties</p>
  </a>
  <a class="quick-button span2" href="#">
    <i class="icon-group"></i>
    <p>Add Faculty Advisors</p>
  </a>
</div>


<div class="row-fluid sortable " id="add" style="display: none">
  <div class="box span12">
    <div class="box-header" data-original-title>

      <h2><i class="halflings-icon white edit"></i><span class="break"></span>Add new User</h2>
      <div class="box-icon">
        <a href="#" class="btn-minimize"><i class="halflings-icon white chevron-up"></i></a>
        <a href="#" class="btn-close"><i class="halflings-icon white remove"></i></a>
      </div>
    </div>
    <div class="box-content">
      <form enctype='multipart/form-data' method="post" class="form-horizontal" action="">
        <div class="control-group">
          <label class="control-label" for="fileInput">Select file</label>
          <div class="controls">
            <input class="input-file uniform_on" id="file" name="file" type="file">
            <input type="submit" class="blue btn" value="Add">

          </div>
          <br>
          <div class="controls">
            <a href="">Or add single user</a>
          </div>
        </div>

      </form>
    </div>
  </div>
</div>
ItayB
  • 10,377
  • 9
  • 50
  • 77
  • cant get it :( wants to display value that u put in alert in a div portion – Sandhya Gor Mar 02 '15 at 07:55
  • I've just update the code. Please check again (instead of alert, I put it in the div header) – ItayB Mar 02 '15 at 07:56
  • Is it what you are looking for? is it OK now? (the java script code was changed) – ItayB Mar 02 '15 at 07:58
  • @user2302126 you haven't said what div you want the text to appear in, from all the divs in your source code do you expect us to do a lucky dip? This shows you how to get the text and how to display it, i think it's time you put some work in and worked out how to place it into "that div" – NewToJS Mar 02 '15 at 07:59
  • is it possible to pass that value to my controller's method as a parameter? – Sandhya Gor Mar 02 '15 at 08:07
  • controller's method?? what is it? – ItayB Mar 02 '15 at 08:08