getting (" placeholder ") input attribute in django - html

I want to get my placeholder input attribute, when a user submit the form. How can it be done in django framework?
for example,
I want it to be done like this:
def get_attr(request):
plh = request.POST.get('placeholder')
I know that the code above isn't correct, because the POST method doesn't have that attribute.
So, what can I do?

It has nothing to do with Django, you can user JS for getting placeholder easily.
Assign a ID to your input field
Using JavaScript
document.getElementById("demo").placeholder
Using Jquery
$('#weight').attr('placeholder')
Then you can use this value in any way you want to

Related

How to prevent old data in Model from being passed into the input fields automatically?

Is it possible to prevent the data from being automatically passed to the input fields. For example - if I have an edit page where I want to change the data and I want the user to type the data again without giving him the pre-filled old data. I was looking over some documentation about the ModelState and the asp-for tag but I just couldn't find the answer.
I was looking for help and I found this post here. Well I want to do exactly the opposite.
asp-for tag helper functionality_
EDIT:
I tried to change the value attribute of the input field to empty string and it worked, however I still would like to know if there is an asp.net approach.
If you don't want to pass data from action to Edit view,you can return a view with an empty model like this:
public IActionResult Edit()
{
return View(new YourModel());
}
Or you can try to use id and name attribute to replace asp-for,so that the data will not be binded to inputs.Here is a demo.
Change
<input asp-for="TestProperty" />
to
<input id="TestProperty" name="TestProperty" />

Get Angular to use initial input data while sending form even if user entered nothing inside input box

I have the following input form component
<input class="form-control" #FirstName="ngModel" name="FirstName" [(ngModel)]="formModel.FirstName" value="{{userProfile?.firstName}}">
Where I render some initial data while rendering page.
My goal is to send this initial data when submiting this form even if nothing was entered inside the form from user. How can I achive it ?
For this particular use case I would suggest you to use reactive forms.As you can use the formControl and add your default value as a part of the formControl.
formItem = new FormControl('default Value');
onSubmit(){
data = this.formItem.value;
}
If we are using the template driven form
The we can use NgSubmit directive and add the resective logic to pick data from the template reference that you have provided.
Template driven forms:
https://angular.io/guide/forms#submit-the-form-with-ngsubmit
Reactive forms
https://angular.io/guide/reactive-forms#step-2-generating-and-importing-a-new-form-control

How to choose a radio button using RCurl in an ajax event

Hi I have isolated an tag containing a radio button and would like to select one of the options. Here is the full input path:
<input type="radio" id="gen" name="gen" value="Male" onclick="ajaxSetAge(this.value);" />
and I am using the following:
postForm("http://www.archersmate.co.uk/",
radio = 'Female')
however this returns:
Error in nchar(str) : invalid multibyte string 1
What am I doing wrong here?
You need to refer to the name of the form field, not the type, like:
postForm('http://www.archersmate.co.uk', gen='Female')
That said, you won't be able to fill out the form on that website because it does not work as an HTTP POST request. Instead, it triggers an AJAX event. So, you're either going to have to go through the javascript and figure out if there's an underlying document you can access directly OR you'll have to use something like PhantomJS to trigger the relevant form fields and record the resulting javascript-generated contents.

How to get GWT textbox value from selenium webdriver?

I'm trying to test my GWT application using selenium, the html generated by GWT Textbox is shown as below:
<input type="text" class="gwt-TextBox" >
No value there, but from UI i can see the text, Is there a way to get the value from selenium?
UPDATE: I can locate the input from selenium, but can not get its value, for example the value of above input is "blahblah...", which i can see from page UI, but can't get from the above html.
#Bhumika is correct, having a unique id attribute for every element you would want to manipulate is good programming practice. But if you don't have that and can't get it added, you still have a good handle on this particular case: the placeholder attribute. To locate the element, use the XPath //input[#placeholder='Input note title...']. To obtain the value of the field, get its value attribute.
Like #BMT said, you should get the value using getAttribute, like this
GWT Code
TextBox textField = new TextBox();
textField.ensureDebugId("textFieldId");
Selenium Code
driver.findElement(By.id("textFieldId")).getAttribute("value");
You could see all the properties (visible or invisible) of one element using Inspect Element Tool of browsers (F12), then get the value you want.
Each widget should have a id for selenium testing. Here selenium does not identify element, and you are not able to get value which are on UI. so you have to set id for input widget.
i.e
TextBox textField= new TextBox();
textField.getElement().setId("name");
If you want the resulting DOM to look like
<input type="text" class="gwt-TextBox" value="myValue">
you'll have to use
textBox.getElement().setAttribute("value", "myValue");
instead of
textBox.setText("myValue")
That's because setText will only update the value property (i.e. theDomElement.value = "myValue"), which will not update the value attribute (i.e. <input value="myValue"/>).
When updating a property, the browser will not update the associated attribute.

How to delete initial field value when user clicks on respective box?

I want to know if its possible writing some code which will delete an initial value from a form when the user clicks on the corresponding form (described in the following example)?
class example(forms.Form):
name = forms.CharField()
email = forms.EmailField()
descr = forms.CharField(initial='Please insert a relevant description ...')
I dont want to use the help_text attribute.
PS: For a better understanding I could make an analogy with java onclick event
One way is to use HTML5's placeholder attribute directly from Django:
descr = forms.CharField(widget=forms.TextInput(attrs={
'placeholder': 'Please insert a relevant description ...'}))
The unique constraint is that it is not yet supported by Internet Explorer.
Because it's a webservice you'll have to use javascript. If you use jQuery, something like this:
$(document).ready(function(){
$('#id_descr').one('click', function(){
$(this).val('');
});
});
for the backend part, have a look at this answer. For the frontend, you may wish to add a html5 shiv to get placeholders in legacy browsers