0

I have a javascript variable

inputElem[i]

when I combine it with other string like:

var result = inputElem[i]  + '<div>xv</div>';

expected result :

<input type="radio" value="www.abc.com"> 
<div>xv</div>

actually :

[object HTMLInputElement] 
<div>xv</div>

4 Answers 4

3

The problem is inputElem[i] is a dom element, so the default to string implementation is to blame here.

Looks like you want the wrapping html markup for the element so use outerHTML

var result = inputElem[i].outerHTML  + '<div>xv</div>';
Sign up to request clarification or add additional context in comments.

1 Comment

downvoter, any reason? This is obviously the only right answer at the moment
1

when, you try to serialize (convert to string) any object, its datatype is what you get as a result

try this:

var result = $('<div>').append(inputElem[i]).clone()).html()+ '<div>xv</div>';

or

 var result = $(inputElem[i]).prop('outerHTML')

or with plain javascript, you can simply do outerHtml i.e.

var result = inputElem[i].outerHTML  + '<div>xv</div>';

Comments

0

In your case, you should concatentate the HTML code for input object, not the input object itself.

Use JQuery html() function:

$(inputElem[i]).html() + '<div>xv</div>';

Comments

0

Try this:

var result = $(inputElem[i]).html() + '<div>xv</div>';
$('#parentID').html(result);

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.