0

The following is a snippet of my form

<span style="font-weight: bold;" id="program_name_1">Basics</span>

<form accept-charset="UTF-8" action="/programs/1" class="edit_program" data-remote="true" id="edit_program_1" method="post">
    <input id="program_name" name="program[name]" size="30" type="text" value="Basics" />
</form>

Here is the jquery I'm trying to use

function set_updated_program_values(id)
{
  $('#program_name_' + id).html($('#edit_program_' + id + ' input:#program_name').val());
  alert('new name is ' + $('#edit_program_' + id + ' input:#program_name').val());
}

The span tag isn't being updated, and the alert says it is undefined. Am I doing something wrong here?

2 Answers 2

2

The problem is the way you're referring to your input. You can use input:#program_name. You should just use #program_name.

Example:

$('#edit_program_' + id + ' #program_name').val()
Sign up to request clarification or add additional context in comments.

1 Comment

You're right, I could have sworn I was doing it like that on another application.
0
  1. You can't have the same IDs for different elements of your HTML code.

  2. Have you linked the set_updated_program_values function to an event? If not, it can't be triggered by itself...

Try this HTML code...

<span style="font-weight: bold;" id="program_name_1">Basics</span>

<form accept-charset="UTF-8" action="/programs/1" class="edit_program" data-remote="true" id="edit_program_1" method="post">
    <input name="program[name]" size="30" type="text" value="Basics" />
</form>

...with this JavaScript:

$(document).ready(function(){

    var changeText = function(){
        var id = this.parentNode.id.split('_')[2];
        var value = this.value.replace(/</g, '&lt;').replace(/>/g, '&gt;');
        $('#program_name_' + id).html(value);
    };

    $('form.edit_program input:text').change(changeText).keyup(changeText);

});

See http://jsfiddle.net/creaweb/BgtBv/ (working example online)

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.