I have input field like
<input type="text" name="time[23][34][]" value="">
name of input field is dynamic. Means it might be time[23][34][],time1[11][33][],time2[45][22][] etc
Now I want to change it to multidimension to singledimension like
time[]
time1[]
time2[]
I have tried below code:
$('input').attr('name').replace(/(\[|\])/g, '\\$1')
How can i replace it using jquery or javascript?
this may help you:
https://jsfiddle.net/s033kv43/
var attrib = $('input').attr('name').replace(/[\[|\]]/g, '');
$('input').attr('name', attrib);
i get the name attribute and replace the brackets with nothing, then set the name attribute.
Dont forget to adjust this code, so its alters the correct input-fields
Related
I want to get a value from JavaScript prompt and pass it to input for finally submit it
HTML
<form id="createdirForm"><input type="text" name="" id="createdir"/><form>
JavaScript
function createdir() {
var newdirname;
newdirname = prompt('Please input the directory name:', '');
if (!newdirname) return;
$('createdir').newdirname.value = newdirname;
$('createdirForm').submit();
}
The reason why it's not working is because you're not calling the selector in your jQuery properly.
You might want to take a look at the jQuery Selector Tutorial.
Now to fix your problem, since you want to get the input and the form by the id attribute, just like in CSS, you need to use the hashtag (#) symbol.
So you would do:
$('#createdir').val(newdirname);
$('#createdirForm').submit();
Here's a working example: https://jsfiddle.net/vm4ah1L3/1/
Just a side note, you might want to add a name to your html input otherwise when you submit it to your PHP, you won't be able to retrieve the value since the name attribute is empty.
<input type="text" name="dirName" id="createdir">
And in your case, since you're declaring the variable newdirname and assign it right after, you could simply do the assignation in one line:
var newdirname = prompt("Please input a directory name: ");
It seems your jQuery selector is wrong. Try
$('#createdir').val(newdirname);
$('#createdirForm').submit();
jQuery uses the css selector logic, therefore to select an element by id you need to put the # before the selector name.
Also, to add a value to an input, you need to use val(param) with your value replaced in the param.
You have some typo and have forgotten to add #. And the function is not executed.
HTML
<form id="createdirForm">
<input type="text" name="dir" id="createdir" />
</form>
JavaScript
function createdir() {
var newdirname;
newdirname = prompt('Please input the directory name:', '');
if (!newdirname) return;
$('#createdir').value = newdirname;
$('#createdirForm').submit();
}
createdir();
I want to change the value of the 'data-list' attribute here :
<input id="Searchbar" data-list="Javascript,Css,Test" data-multiple/>
I tried :
Document.getElementById('Searchbar').XXXXX
All this stuff but I can't find the right way to do it.
Any suggestions?
The document is case sensitive. Try:
document.getElementById('Searchbar').XXXXX
And to set the data-list attribute, you need to do:
document.getElementById('Searchbar').setAttribute("data-list", "new,list");
I have a situation where class and id are taken, but I need to pass a variable in jQuery. How do you recommend?
HTML
<input
type="text"
id="cannot_change_this"
class="rather_not_append_extra_classes_just_for_a_variable"
value="Full Text Name"
hash="ABCDE">
Each value, when selected via autocomplete has an associated hash. In the example above the value might be Full Text Name, but this is not unique. What I really need to pass is the hash: ABCDE.
I can't change the id and I'd rather not add extra classes and have to filter out the ones used for styles to get my hash. And since it's an input I can't use .text() either.
How else can I pass ABCDE hash to jQuery?
Look at using jQuery's data method
http://api.jquery.com/jQuery.data/
You can calculate the hash of the value in javascript, then set it to the input's data.
Also if using HTML5, you can specifically declare data attributes in the HTML. So you could use a property like data-hash=HASH to set your data.
Alternatively, if you don't care about comliance or maybe you aren't using HTML5, you could just set your own attribute like
<input type="text" id="cannot_change_this"
class="rather_not_append_extra_classes_just_for_a_variable"
value="Full Text Name" hash="ABCDE" />
And read it with jQuery like this:
var hash = $('input#idOfInput').attr('hash');
If i got it right.
Setting the value:
$("#cannot_change_this").data("hash", hashValue);
Getting the value later:
$("#cannot_change_this").data("hash");
You could just use the title attribute:
<input type="text" id="cannot_change_this"
class="rather_not_append_extra_classes_just_for_a_variable"
value="Full Text Name"title="ABCDE" />
it is a standard attribute for all inputs.
you can have something like:
jQuery(document).ready(function(){
//SET:
jQuery("#cannot_change_this").attr("new-value", "ABCDE");
//READ:
var val = jQuery("#cannot_change_this").attr("new-value");
alert(val);
});
I have a large string in a variable that includes a whole bunch of HTML tags.
I want to get the value of a hidden input field within the string and store it in its own var.
<input type="hidden" value="WantThis" />
Can anyone help me out at all?
You can parse the HTML with jQuery to get the value:
var theValue = $(myString).find('input[name=something]').val();
I'm assuming the hidden field has a name. If it doesn't, you'll need to specify input[type=hidden] and find it using its position relative to the rest of the content.
If your string does not already have a root element and the <input> is not nested, you'll probably want to use $('<div>' + myString + '</div>') instead.
Get the hidden input like so:
$(html).find("input[type=hidden]").val()
Create an ID for the hidden input and call it like normal
<input type="hidden" value="WantThis" id="myInput" />
Then call it
var myval = $('#myInput').val();
Check input tag which have value only as <br/> tag inside as value using jQuery
I am using an htmlEditor in my project, sometimes user just press enter key only in textarea field. It will get as <br/><br/><br/><br/> as value when saving.
I want to avoid this tags when saving.
How to validate this in jquery ?
If any other characters like <br/>Just <br/> Example <br/> is present then I want to save it with break tag itself.
using a regex like this would help.
/^(\<br\/>)+$/.test($("textarea").val());
Clean it like this:
// Get the HTML and remove <br /> variants
var htmlCleaned = $('#MyTextArea').val().replace(/<br\s?\/?>/, '');
Refer LIVE DEMO
var divTag = $("div");
var nDivTag = $(divTag[0].outerHTML);
$("br", nDivTag).remove();
alert(nDivTag.html());