Access div hidden parameter - javascript

I'm iterating over a div using :
Can I access a hidden parameter in this div within the iteration ?
<DIV>
<div id="myDiv" <input type="hidden" name="Language" value="English"> />
</DIV>
$('#myDiv div.id').each(function() {
//access a hidden parameter of the current div here
});

You can use one of the following methods:
$('#myDiv div.id').each(
function(){
parameter = $(this).attr('data-hidden-parameter');
/* or:
parameter = $(this).data('hidden-parameter');
*/
});
The first parameter = $(this).attr('data-hidden-parameter') requires the following structure:
<div class="id" data-hidden-parameter="value"><!-- other stuff --></div>
Whereas the latter works with getting/setting with the data() method of jQuery:
$(selector).data('hidden-parameter','value'); // sets
$(selector).data('hidden-parameter'); // gets
If you mean retrieving text, or other content/attributes, from a hidden element that's a child of the div.id element, with mark-up such as:
<div class="id">
<input type="hidden" value="somethingOrOther" />
<span style="display: none;">Some text in a hidden element</span>
</div>
You could retrieve that value with:
$('#myDiv div.id').each(
function(){
parameter = $(this).find('input:hidden').val();
/* or:
parameter = $(this).find('span').text();
});
Note your jQuery selector implies you're iterating over a number of elements, based on the class of those elements, while your class-name, id, implies you're trying, instead, to search based on the id of an element. This might be pseudo-code to demonstrate your approach, but please don't use a class-name of id. It's perfectly valid, but it's terribly confusing. Albeit this is simply my own, personal, objection and response.
Edited, to supply a more targeted answer, by amending one of the above suggestions with an appropriate selector:
var parameter = $('#myDiv').find('input:hidden[name="Language"]').val();
References:
attr().
data().
find().
:hidden selector.
text().
val().

your html code is invalid: if you mean
<div id="myDiv"><input type="hidden" name="Language" value="English"></div>
this will target your input
$('#myDiv input[type="hidden"]')

You cannot put a tag inside another tag (or, in HTML or HTML-compatible XHTML, use self-closing tag syntax on a div). Your HTML is invalid and subject to whatever error recovery parsers care to put it though.
If you had, for instance:
<div id="myDiv">
<div class="id">
<input type="hidden" name="Language" value="English">
</div>
</div>
Then you could do:
$('#myDiv div.id').each(function(index, element) {
var foo = $(element).find('input').val()
});
… but you are probably better off using data-* and jQuery's data() method

Related

how to append a <hr> to a form element selected in JQuery?

Some introduction:
I'm creating a form in Django.
In my template I'm trying to select the final element of that form and attach a <hr> html element to help clean up the visual clutter.
I've tried selecting the Forms ID and inserting adjacenthtml as well as appending raw html. I have also tried passing raw html into bootstrap in my form but this has also failed to render.
tldr; I am trying to append a <hr> element to something I am targeting in Jquery
function addHoriztonalRule() {
document.getElementById("form").insertAdjacentHTML(
"<hr>");
}
You failed to add the position of where you wish to place the HTML in relation to the element.
MDN: A DOMString representing the position relative to the element; must be one of the following strings:
'beforebegin': Before the element itself.
'afterbegin': Just inside the element, before its first child.
'beforeend': Just inside the element, after its last child.
'afterend': After the element itself.
function addHoriztonalRule() {
document.getElementById("form").insertAdjacentHTML('afterend', '<hr>');
}
addHoriztonalRule()
<form id="form" method="post">
<input type="button" value="button">
</form>
With jquery you can use this:
function addHoriztonalRule() {
$('#form').after("<hr>");
}
addHoriztonalRule();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form id="form">
<input type="text" value="hi" >
</form>
However, it is better to share the structure of the page to learn more from DOM and how to call the function to better guide

The functions .prev() and .closest() not working as expected

This is my .cshtml code :
<div class="col-xs-4 col-md-2">
<div class="form-group">
<input asp-for="FileName" type="hidden" class="hdnFileName" />
<input type="file" asp-for="UploadFile" accept=".csv" />
<button id="btnUpload" class="btn btn-tertiary btnFileSelect" type="button">Browse</button>
</div>
</div>
I need the id of the FileName on click of the upload button.
Script :
$(".btnFileSelect").each(function () {
var btnId = $(this).attr("id");
//The below code is not working and throwing an undefined value.
//Tried .closest() which is not working either.
var fileName = $(this).prev('.hdnFileName').attr('id');
})
Variable fileName is showing undefined.
What do I change? Is it being a hidden field the issue?
prev doesn't scan, it will only ever return a jQuery object for the previous element (if the selector matches) or an empty one (if it doesn't). From the documentation:
Get the immediately preceding sibling of each element in the set of matched elements. If a selector is provided, it retrieves the previous sibling only if it matches that selector.
(jQuery's set-based nature makes that slightly less clear than it could be, but basically you'll get the immediately-previous element if it matches, or none if it doesn't.)
If you want to scan, use .prevAll().eq(0). Or in this case, I'd probably use siblings:
var fileName = $(this).siblings('.hdnFileName').attr('id');
(I'm assuming something at some point adds an id to that element, since it doesn't have one in the markup you've shown.)

Replacing a number found on multiple instances in div element with another number

As you can see, my HTML contains multiple references to '0'. I need to change these to '1'.
However, my jQuery isn't working.
jQuery(".create-new-location").click(function() {
jQuery("#header-logo").html().replace(/\[0\]/g, '['+(1)+']');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="header-logo" class="header-title location-header-0 title-edit-header" data-row-id="location-header-0" title="Location name (for your reference)">
<div class="input-selection title-edit-header">
<div class="text-input">
<input class="option_textbox col-sm-12 change-width title-edit" placeholder="Location name (for your reference)" value="" type="text" name="bp_theme_options[social][map][locations][0][location_name]">
</div>
</div>
<div class="open-block pencil-edit" data-row-id="location-header-0"></div>
</div>
You have to set the html like this
jQuery(".create-new-location").click(function() {
var the_html = jQuery("#header-logo").html().replace(/\[0\]/g, '['+(1)+']');
jQuery("#header-logo").html(the_html);
});
But this is not a good practice!!
When you need to change only the attribute of an <input>, why change the whole #header-logo, right? When you re-draw html like this, you risk losing event-handlers binded to the elements you have just re-drawn.
jQuery(".create-new-location").click(function() {
var elements = jQuery("#header-logo").find('input[name]'); /*all input with name*/
elements.each(function(el){
var the_name = el.attr('name').replace(/\[0\]/g, '['+(1)+']');
el.attr('name', the_name);
});
});
Regexing the html is never a good idea.
As you can see, my HTML contains multiple references to '0'. I need to change these to '1'.
The approach you used, and even the accepted answer here, will not modify the containing div with id="header-logo" which contains several of these references. Moreover, there are significant issues with simply replacing existing dom elements with freshly regexed ones in validation cases (as in, this may break your validation).
The approach you should use is to specifically target the attributes that contain these references, and then only modify those. Here is a general approach which looks in all attributes and modifies the occurrence of [0 (0 being the value of before) into [1 (1 being the value of after) as well as modifying the occurrence of -0 (before = 0) to -1 (after =1).
This will prevent removing any existing event handlers from the elements, as well as a number of other issues associated with regexing straight html and then replacing the dom element with the that result.
$.fn.indexUpdate = function(before,after){
$("*",this).add(this).each(function(){
$(this.attributes).each(function(){
this.value = this.value.replace(new RegExp('\\b\\-'+before+'\\b','g'), '-'+after);
this.value = this.value.replace(new RegExp('\\['+before, 'g'), '['+after);
});
});
};
$("#header-logo").indexUpdate(0,1);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="header-logo" class="header-title location-header-0 title-edit-header" data-row-id="location-header-0" title="Location name (for your reference)">
<div class="input-selection title-edit-header">
<div class="text-input">
<input class="option_textbox col-sm-12 change-width title-edit" placeholder="Location name (for your reference)" value="" type="text" name="bp_theme_options[social][map][locations][0][location_name]">
</div>
</div>
<div class="open-block pencil-edit" data-row-id="location-header-0"></div>
</div>
This statement jQuery("#header-logo").html().replace(/\[0\]/g, '['+(1)+']'); retrieve the html inside the element that have id as header-logo and replace every 0 inside the html string with 1 But it doesn't assign the modified string again to the element So you may want to use following code.
jQuery("#header-logo").html(jQuery("#header-logo").html().replace(/\[0\]/g, '['+(1)+']'));
Try this:It will replace all existence of '0' with '##'
$(".create-new-location").click(function() {
$("#header-logo").html().replace(/0/gi, '##')
});

How to get a HTML element in a HTML element array with JQuery

I have this code that gives me all the LABEL inside the HTML element with id "nuovaImmagine":
$.labelImmagine = $("#nuovaImmagine1").find("label");
I know that inside $.labelImmagine there are 3 labels. If I do alert($.labelImmagine.size()); the alert shows "3";
Now I have to get the first element of the array and edit the text of the label.
I tried both $.labelImmagine.get(0).text("Hello") and $.labelImmagine[0].text("Hello") but none works.
Any idea?
Thank you
You don't want the HTML element, you want the jQuery object of that HTML element. I can tell because you're trying to use jQuery methods on it.
Both $().get(0) and $()[0] give you DOM nodes. You need $().eq(0) or $().first(), which return a jQuery object.
It's a bad idea to pollute the $ namespace. I'd rather use
var $labelImmagine = $("#nuovaImmagine1").find("label");
// ^--- no dot.
But yea.... otherwise, simply do like
$.labelImmagine = $("#nuovaImmagine1").find("label");
$.labelImmagine.eq(0).text("WOOOOO");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="nuovaImmagine1"><label>TEST</label></div>
and all wrapped inside an IIFE or document ready scope:
jQuery(function($) { // DOM ready and $ alias secured
/**/
});
You can use .eq() and change its text with .text()
$.labelImmagine = $("#nuovaImmagine1").find("label");
$.labelImmagine.eq(0).text('some text');
Check the below snippet
$.labelImmagine = $("#nuovaImmagine1").find("label");
$.labelImmagine.eq(0).text('some text')
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="nuovaImmagine1">
<label>label 1</label>
<br>
<label>label 2</label>
<br>
<label>label 3</label>
<br>
</div>
If you want to continue using Jquery you would need to re-'jqueryify' the element.
For example
$($.labelImmagine[0]).text("Hello")
This is because by selecting the element by index it is no longer a jquery object, and thus .text() doesn't exist.
Otherwise you could use
$.labelImmagine[0].innerHTML = "Hello"

Prototype - Element with styleclass in element with an id?

First of all: I'm new to Prototype JS Framework!
Until now I worked with jQuery.
In jQuery I am able to get an element by coding:
$('#myitemid .myitemclass').val()
html:
<div id="myitemid">
<input type="text" class="notmyclass" />
<input type="text" class="myitemclass" />
<input type="text" class="notmyclass" />
</div>
But how to do this in prototype?
I tried to code:
$('myitemid .myitemclass').value
but this won't work.
Can U help me plz?
Use $$ which returns all elements in the document that match the provided CSS selectors.
var elemValue = $$('#myitemid input.myitemclass')[0].getValue();
Also input.myitemclass is better than .myitemclass because it restricts search to input elements with class name .myitemclass.
If you want to get the named element myitemid, simply use $('myitemid'). This is equivalent to $('#myitemid') or document.getElementById('myitemid'). Your case is more complex, since you want to select a child of a named element. In that case you want to first find the named element, then use a selector on it's children.
$('myitemid').select('input.myitemclass')
Then, to access it's value (since it's a form element), you can add .getValue().
$('myitemid').select('input.myitemclass').getValue()
Should be faster
$("myitemid").down("input[class~=myitemclass]").value

Categories

Resources