Thomas
Thomas

Reputation: 5099

JS Replace Text

I have a drop down menu that looks like this:

alt text

It works fine, but I need to replace the text "Choose an Action" with whatever link the user selects from the box. What is the best way to handle this?

Here is the code for the drop down:

$("#dd_open a").click(function(event) {
event.preventDefault();
$("#dd_open a").removeClass('selected');
$(this).addClass('selected');
return false;
}); 

$("#dd_btn").click(function(event) {
window.location.href = $("#dd_open a.selected").attr('href');
}); 

Upvotes: 0

Views: 224

Answers (2)

griegs
griegs

Reputation: 22770

$('#ChooseAnActionElement').val( $(this).val() );

The above will replace the contents of an element with the contents of the selected element.

Or as @slaks has done

$('#ChooseAnActionElement').text( $(this).text() );

depending on the element.

Upvotes: 1

SLaks
SLaks

Reputation: 888167

You can set the text of the Choose an Action element by calling $('some selector').text(something).

You can get the text of the clicked element by calling $(this).text() in the click handler.

Upvotes: 1

Related Questions