0

HTML is:

<div class="form-item" id="edit-image-upload-wrapper"><img id="img_prev" src="#">
     <label for="edit-image-upload">Upload Your Picture: <span class="form-required" title="This field is required.">*</span></label>
     <input type="file" name="files[image_upload]" class="form-file required" id="edit-image-upload" size="60"></input>

     <div class="description"><p>You need to provide a passport sized image in jpg/jpeg or png format. The image size has to be 100kb or lower.</p>
    </div>
    </div>

JS code:

$('#edit-image-upload-wrapper input').prepend('onchange="readURL(this);"');

due to this JS code input tag has been changed as follows:

<input type="file" name="files[image_upload]" class="form-file required" id="edit-image-upload" size="60">onchange="readURL(this);"</input> 

I want to change as follows:

 <input type="file" name="files[image_upload]" class="form-file required" id="edit-image-upload" size="60" onchange="readURL(this);"></input> 

How can I do this?

2
  • Did you tried to use .attr() instead of .prepend()? Commented May 10, 2014 at 11:30
  • first thing <input> is a singular tag, means it has no end tag Commented May 10, 2014 at 11:31

2 Answers 2

2

In general, to add (or update) an attribute to an element, you can use attr:

$('#edit-image-upload-wrapper input').attr('onchange', 'readURL(this);');

As that attribute is reflected as a property, prop would also work:

$('#edit-image-upload-wrapper input').prop('onchange', 'readURL(this);');

However, as you're using jQuery, I can't see any good reason for doing this as opposed to using proper event hookup, such as:

$('#edit-image-upload-wrapper input').on('change', function() {
    readURL(this);
});

And if you can change the readURL function so that it gets the URL from this rather than from its first argument, that can be simpler:

// Requires small change to `readURL`
$('#edit-image-upload-wrapper input').on('change', readURL);

Side note 1: Your input has its own id value (edit-image-upload), so those selectors could be simply #edit-image-upload, rather than #edit-image-upload-wrapper input.


Side note 2: Your HTML is invalid. input elements are "void" elements, they never have content, and so you never write an ending tag for them. E.g., you never write </input>. Any element that cannot have content (input, br, hr, and a few others) is never written with an end tag.

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

2 Comments

Uncaught TypeError: Object #<Object> has no method 'on'
@AbdusSattarBhuiyan: Wow, you're using a really outdated copy of jQuery. I strongly recommend updating to something more recent. on was added in v1.7.0 nearly three years ago (current versions are v1.11.1 and v2.1.1). Anyway, with older versions it would be .bind("change", function... or in either version, just .change(function....
0

Use this:

$('#edit-image-upload-wrapper input').change(function() {
     //.....
});

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.