I have an array like this:
["school_name":"My School Name", "school_number":"54546", "note":"", "class":1]
and HTML like this:
<input type="text" name="school_name" />
<input type="text" name="school_number" />
<textarea name="note" ></textarea>
<select name="class">
<option value="1"> One </option>
<option value="2"> Two </option>
<option value="3"> Three </option>
</select>
I want to get a tag’s name from its attribute name:
$('*[name=school_name]').attr("tag"); // this is input
$('*[name=class]').attr("tag"); // this is select
$('*[name=note]').attr("tag"); // this is textarea
Note that I have tried this:
$('*[name=school_name]').attr("tag");
$('*[name=school_name]').prop("tag");
$('*[name=school_name]').get(0).tagName;
$('*[name=school_name]')[0].tagName;
This is what I want to do:
If the tag name is input text,
$("input[name=school_name]").attr("value", "My School Name");
If the tag name is a select box,
$("select[name=class]").children("option[value=2]").attr("selected", "selected");
This is demonstrated in this fiddle.