I have a simple form with 4 inputs that I need to dynamically add the placeholder into.
I think i understand the general idea of json, but I’m not sure how to make this work. Can someone please help me?
var inputArray = [
{"inputClass":"name","placeholder":"Your Name*"},
{"inputClass":"phone","placeholder":"Your Phone*"},
{"inputClass":"email","placeholder":"Your Email*"},
{"inputClass":"message","placeholder":"Your Message*"}
];
$.each(inputArray, function(i, inputClass, placeholder) {
$('form').find('input.'+inputArray[i].inputClass).attr("placeholder", inputArray[i].placeholder);
});
Nothing wrong with your code, except your signature for the
.each(...)callback is a bit wrong. It should befunction(index,element) { ... }. But your code works anyway. Cleaner way to write your code would be:Working Fiddle: http://jsfiddle.net/RQAJA/