Walrus
Walrus

Reputation: 20464

Jquery Inner HTML properties appear non functional

The following code should populate the value of several inputs with the same name with that of the inner html of a div but appears not to be populating the value of those inputs with the inner html of the div.

 var specsort = $('#specifications').html();
$('input[name="GT_specifications"]').each(function(){
    $('input[name="GT_specifications"]').val(specsort);
    });

Any ideas,

Marvellous

Upvotes: 1

Views: 336

Answers (3)

Stevie Howlin
Stevie Howlin

Reputation: 1

TJ and inti are right. However, I've just tested the code you posted and it appears to be working for me. Are you wrapping the code in the $(document).ready syntax to ensure the page is fully loaded before the jQ fires?

e.g.

$(document).ready(function() {
    var specsort = $('#specifications').html();
    $('input[name="GT_specifications"]').each(function(){
        $('input[name="GT_specifications"]').val(specsort);
    });
});

Upvotes: 0

T.J. Crowder
T.J. Crowder

Reputation: 1074949

Did you mean:

var specsort = $('#specifications').html();
$('input[name="GT_specifications"]').each(function(){
    $(this).val(specsort);
});

Or, actually:

$('input[name="GT_specifications"]').val($('#specifications').html());

(The quotes around "GT_specifications" are optional.)

Live copy

That will retrieve the inner HTML of the element with the ID "specifications" and set it as the value on each input with the name "GT_specifications".

Upvotes: 3

aorcsik
aorcsik

Reputation: 15552

First, you are iterating through the input elements, and in every one, you set all of them all over again. Use the first wrapper.

var specsort = $('#specifications').html();
$('input[name="GT_specifications"]').val(specsort);

Hope this helps!

Upvotes: 0

Related Questions