0

I'm building a dropdown menu, and I want to select any of the lis from the parent element of ul when clicked. Then, take that text and change the "Work Inquiry" (text of #subject) text w/ selected menu item text.

I have comments explaining my predicament on the fiddle.

Here is the jsFiddle :: http://jsfiddle.net/8m4Tj/

$("ul#subjects > li").on("click", function () {
  $("#subjects").slideUp(210);
  var liText = $(this).text();
  var newSubjectText = $("#subject").text();
  alert(liText); // this alerts selected item... how do you change #subject.text ???
  alert(newSubjectText); // stays the same
});  

I don't know if im selecting the wrong item, but I want to replace the data-value and the actual text value. I'm doing this method since there are no easy ways of styling the <select> element.

1
  • 1
    $("#subject").text(liText); Commented Jun 11, 2014 at 7:09

4 Answers 4

7

Use the setter version of .text()

$("ul#subjects > li").on("click", function () {
    $("#subjects").slideUp(210);
    $("#subject").text($(this).text());
});

Demo: Fiddle

Sign up to request clarification or add additional context in comments.

Comments

2

Use .text() to set the text to span element

Try this

$("ul#subjects > li").on("click", function () {
                $("#subjects").slideUp(210);
                var liText = $(this).text();
                $("#subject").text(liText); // this line set the text
                alert(liText);
            });

DEMO

Comments

1

Try this, this will update data value also

$("ul#subjects > li").on("click", function () {
    $("#subjects").slideUp(210);
    $("#subject").text($(this).text()).prop('data-value' $(this).text());
});

Comments

1

Try this:

$("ul#subjects > li").on("click", function () {
    $("#subjects").slideUp(210);
    var liText = $(this).text();
    $("#subject").text(liText);
    var newSubjectText = $("#subject").text();
    alert(liText);
    alert(newSubjectText); 
});

Working Fiddle

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.