0

Ok so this i probably very easy but i can't seem to figure it out. I have a a function but i still have to call the function inline. I am wondering if there is a way to put it all in the function. Right now my function looks like this:

function ddItem(el) {
 $(el).closest(".ddSelectHeader").find("input").attr({
    value : $(el).text()
})
 }

Then i call this function inline like this:

<div class="ddContainer">
<div class="ddSelectHeader"><input name="" type="text" />
<div class="ddSubmenu">
  <a href="#" onclick="ddItem(this)">Item 1</a><br />
  <a href="#" onclick="ddItem(this)">item 2</a>
  </div>
</div>

Ok so i am trying to make a simple drop down but with divs instead of the traditional ul li. The way it is written above works fine but i was hoping there is a way to take the onlcick out so that any link that is click within the ddSubmenu will populate the input field. This way i don't have to call it inline or in the html.

Thanks for the help.

4 Answers 4

4
$(document).ready(function() {
    $(".ddSubmenu a").click(function(){
        var $this = $(this);

        $this.closest(".ddSelectHeader").find("input").val($this.text());
    });
})
Sign up to request clarification or add additional context in comments.

Comments

1
$(function(){
    $('.ddSubmenu > a').click(function(){
        var self = $(this);
        self.closest(".ddSelectHeader").find("input").val(self.text());
    });
});

3 Comments

+1 for value, I fixed the structure but missed what he was actually setting.
@JamesMontagne - You can still modify your answer and make it better.
@ShankarSangoli Will do. Always feel funny amending my answer based on someone elses prior to an answer being accepted.
0

Via javascript, you can use element.onclick: https://developer.mozilla.org/en/DOM/element.onclick

or jQuery's .click(): http://api.jquery.com/click/

Comments

0
$('.ddSubmenu a').click(function(){
    ddItem(this);
}); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.